BeforeSave Check two ranges cancel if one is empty and the other not

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:

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
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top