VBA BeforeCLose

excelgal2016

New Member
Joined
Dec 14, 2016
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I have the below code and this is what I want to achieve:

IF L1=1 AND B1="" AND C1=""

then I want to prevent the file from saving. In all the other scenarios, the file should be saved. I need to also be able to specific Sheet name.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


If (Sheets("Sheet1").[L1] = "1" And Sheets("Sheet1").[B1] = "" And Sheets("Sheet1").[C1] = "") Then


Cancel = True
MsgBox "Save disabled. Custom message comes here"
Else
Cancel = False
End If
End Sub

This code works but it disregards cell C1. I also want to be able to add more cells that can't be blank if required.

Anyone who can help?

Thank you
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Respectively, if anyone knows a better solution, this is what I'm looking to achieve:

If cell A1 is empty then we're all good. Document saves normally.

BUT

If cell A1 has a value, then cells B1, C1, ... need to be completed as well, otherwise document won't save.
 
Last edited:
Upvote 0
Are you sure you want an AND for the second one and not an OR.

With AND then both B1 & C1 need to be empty with OR it will stop if either are. the code you posted looks like it will work and even works with a quick test.

To make it easier for adding more conditions consider using a with:

Code:
With Sheets("Sheet1")
    If (.[L1] = "1" And (.[B1] = "" OR .[c1] = "")) Then
        Cancel = True
        MsgBox "Save disabled. Custom message comes here"
    End If
End With

You shouldn't need the else statement as the Boolean is false by default.
 
Last edited:
Upvote 0
Are you sure you want an AND for the second one and not an OR.

With AND then both B1 & C1 need to be empty with OR it will stop if either are. the code you posted looks like it will work and even works with a quick test.

To make it easier for adding more conditions consider using a with:

Code:
With Sheets("Sheet1")
    If (.[L1] = "1" And (.[B1] = "" OR .[c1] = "")) Then
        Cancel = True
        MsgBox "Save disabled. Custom message comes here"
    End If
End With

You shouldn't need the else statement as the Boolean is false by default.

It works with OR. Thank you! :)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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