VBA BeforeCLose

excelgal2016

New Member
Joined
Dec 14, 2016
Messages
14
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
 

excelgal2016

New Member
Joined
Dec 14, 2016
Messages
14
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:

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
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:

excelgal2016

New Member
Joined
Dec 14, 2016
Messages
14
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! :)
 

Forum statistics

Threads
1,082,045
Messages
5,362,862
Members
400,696
Latest member
Kclynn

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top