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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to the board
Try
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


    For i = 1 To 32
        With Sheets("PI" & i)
            If WorksheetFunction.CountA(.Range("I14:J14")) <> 2 _
                    Or WorksheetFunction.CountA(.Range("I17:J17")) <> 2 Then
                MsgBox Msg
                Cancel = True
            End If
        End With
    Next i
    
End Sub
 
Upvote 0
ahh, i see. yes they are merged cells. the first range to check is the i14:j14 and the second range is the i17:j17 (both merged cells)
 
Upvote 0
In that case try
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


    For i = 1 To 32
        With Sheets("PI" & i)
            If WorksheetFunction.CountA(.Range("I14:J14")) <> 1 _
                    Or WorksheetFunction.CountA(.Range("I17:J17")) <> 1 Then
                MsgBox Msg
                Cancel = True
                Exit Sub
            End If
        End With
    Next i
    
End Sub
 
Upvote 0
No that didn't work either. I don't think Or will work. I'm trying to get it to Cancel only if one range has a value And the other doesn't. Wouldn't I have to use an And statement?
 
Upvote 0
Give this a go
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
    Dim Rng As Range


    For i = 1 To 32
        With Sheets("PI" & i)
            If WorksheetFunction.CountA(Union(.Range("I14"), .Range("I17"))) = 1 Then
                MsgBox Msg
                Cancel = True
                Exit Sub
            End If
        End With
    Next i

End Sub
ps: At the moment this will quit as soon as a sheet is found that is "wrong".
Is this what you want, or would you prefer to check all 32 sheets & then report which of those sheets is wrong?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,841
Members
449,051
Latest member
excelquestion515

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