MoutainManThan
New Member
- Joined
- Sep 3, 2017
- Messages
- 7
Hi, I'm fairly new to vba and have been trying to create a sub that when the workbook is saved it checks two different ranges. If either one is empty and the other has a value in it cancel save and display a msg. if both are empty or both have values continue with save. The workbook has a master sheet (which stays very hidden), a summary sheet and then 32 copies of the master sheet. Ideally I want to check all 32 copies. The data in the ranges should just be integers if that make any difference. I've tried Select Case, If Then statements, If And Then statements, and all sorts of random stuff I've found online. I'm not receiving any error messages, but the sub is not preventing me from saving or displaying the msgbox when the right conditions are met.
The Code I have right now is just pieces of different attempts I've made. Some old stuff is commented out. any suggestions or tips welcome.
here's my code:
The Code I have right now is just pieces of different attempts I've made. Some old stuff is commented out. any suggestions or tips welcome.
here's my code:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Msg As String
Msg = "Wrong Form. Try Again."
Dim i As Integer
If SaveAsUI Then
For i = 1 To 32
If IsEmpty(Sheet("PI" & i).Range("I14:J14")) And IsEmpty(Sheet("PI" & i).Range("I17:J17")) = False Then
MsgBox Msg
Cancel = True
Next i
End If
'If IsEmpty(Range("I14")) = True And IsEmpty(Range("I17")) = False Then
'MsgBox Msg
'Cancel = True
'End If
'If IsEmpty(Range("I14")) = False And IsEmtpy(Range("I17")) = True Then
'MsgBox Msg
'Cancel = True
'End If
End If
End Sub