I'm trying to do a beforesave event, not allowing users to save if one of two given cells are empty. What I managed to do so far is linking column 13 (M) and cell A4.
What I'd like to do is applying the event to a combination of two range and rows, A4-A19 and M4-M19. In this way: If A4 is not empty and M4 is empty, a msgbox appears and blocks saving and so on..A5-M5, A6-M6...until A19-M19. If both corresponding cells are empty at the same time, then saving should be possible.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim i As Integer, MyWb As Object
i = 13
Set MyWb = ThisWorkbook.Sheets("Planning").Cells
Do While MyWb(4, i).Value <> ""
i = i + 1
Loop
If i = 13 Then
If ThisWorkbook.Sheets("Planning").Range("A4") <> "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
End If
End Sub
Based on Wolfie's code, I managed to obtain what I wanted, just adding a If not isempty for A column and replacing 19 instead of 13.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim plansht As Worksheet
Set plansht = ThisWorkbook.Sheets("Planning")
' Loop over rows
Dim rw As Integer
For rw = 4 To 19
' Test if both the A and M column in row "rw" are blank
If Not IsEmpty(plansht.Range("A" & rw)) And plansht.Range("M" & rw).Value = "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
Next rw
End Sub