Bennettc18
New Member
- Joined
- Aug 1, 2016
- Messages
- 3
Hello I've been having trouble with one of the macros I have set up through VBA. There are 3 macros (all which are practically the same) for 3 different sheets on the same excel file. I was wondering what I should add to my code of what I should do to make my code work. It works if I put it in the workbook but I can't do that because the first sheet needs to input in more cells than the others.
This is my code.
I can edit anything to help make this more clear. Please and Thank you!
This is my code.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = False
Application.EnableEvents = True
'Row 1
If Trim(Range("A2")) <> "" Then
If Trim(Range("C2")) = "" Then
Cancel = True
Message = Message & "Cell C2 must be filled in" & Chr$(13)
End If
If Trim(Range("D2")) = "" Then
Cancel = True
Message = Message & "Cell D2 must be filled in" & Chr$(13)
End If
End If
'Row 2
If Trim(Range("A3")) <> "" Then
If Trim(Range("C3")) = "" Then
Cancel = True
Message = Message & "Cell C3 must be filled in" & Chr$(13)
End If
If Trim(Range("D3")) = "" Then
Cancel = True
Message = Message & "Cell D3 must be filled in" & Chr$(13)
End If
End If
'Row 3
If Trim(Range("A4")) <> "" Then
If Trim(Range("C4")) = "" Then
Cancel = True
Message = Message & "Cell C4 must be filled in" & Chr$(13)
End If
If Trim(Range("D4")) = "" Then
Cancel = True
Message = Message & "Cell D4 must be filled in" & Chr$(13)
End If
End If
'Row 4
If Trim(Range("A5")) <> "" Then
If Trim(Range("C5")) = "" Then
Cancel = True
Message = Message & "Cell C5 must be filled in" & Chr$(13)
End If
If Trim(Range("D5")) = "" Then
Cancel = True
Message = Message & "Cell D5 must be filled in" & Chr$(13)
End If
End If
'Row 5
If Trim(Range("A6")) <> "" Then
If Trim(Range("C6")) = "" Then
Cancel = True
Message = Message & "Cell C6 must be filled in" & Chr$(13)
End If
If Trim(Range("D6")) = "" Then
Cancel = True
Message = Message & "Cell D6 must be filled in" & Chr$(13)
End If
End If
'Add more if needed
If Cancel = True Then MsgBox Message
End Sub
I can edit anything to help make this more clear. Please and Thank you!
Last edited by a moderator: