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
You can loop over the rows, and just test the A
and M
columns to test if they are both blank for a given row. See the below code...
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 13
' Test if both the A and M column in row "rw" are blank
If plansht.Range("A" & rw).Value = "" 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
Edit:
Your edit suggested you want some different combinations of the cells being empty. Here are some examples of the If
statement for different outcomes
' If BOTH are empty
If plansht.Range("A" & rw).Value = "" And plansht.Range("M" & rw).Value = "" Then ...
If IsEmpty(plansht.Range("A" & rw)) And IsEmpty(plansht.Range("M" & rw)) Then ...
' If EITHER is empty
If plansht.Range("A" & rw).Value = "" OR plansht.Range("M" & rw).Value = "" Then ...
If IsEmpty(plansht.Range("A" & rw)) Or IsEmpty(plansht.Range("M" & rw)) Then ...
' If BOTH are NOT empty
If plansht.Range("A" & rw).Value <> "" And plansht.Range("M" & rw).Value <> "" Then ...
If Not IsEmpty(plansht.Range("A" & rw)) And Not IsEmpty(plansht.Range("M" & rw)) Then ...
Notice that when you start introducing Not
with multiple conditions, the logic can quickly become hard to interpret. You can use brackets to group conditions with a Not
, but you get things like this meaning the same logically:
If Not IsEmpty(plansht.Range("A" & rw)) And Not IsEmpty(plansht.Range("M" & rw)) Then ...
If Not (IsEmpty(plansht.Range("A" & rw)) Or IsEmpty(plansht.Range("M" & rw))) Then ...