Mandatory Cells Before Saving; IF Statement: Message box problem

Maik

New Member
Joined
Oct 16, 2012
Messages
10
Hi there, I'm new in the forum. Hope some kind soul helps :)

I wrote this code without many idea of VBA thanks to the search tool:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
Dim msg As String
With Sheets("Form")
    If WorksheetFunction.CountIf(.Range("C5"), "Creation") <> 0 Then
        If .Range("C6").Value = "" Then msg = msg & "Select Customer Group" & vbNewLine
        If .Range("C7").Value = "" Then msg = msg & "Select Code" & vbNewLine
        If .Range("C11").Value = "" Then msg = msg & "Enter Company or Family Name" & vbNewLine
        If .Range("B12").Value = "First Name" And .Range("C12").Value = "" Then msg = msg & "Enter First Name" & vbNewLine
        If .Range("C13").Value = "" Then msg = msg & "Enter Address" & vbNewLine
        If .Range("C14").Value = "" Then msg = msg & "Enter Postal Code" & vbNewLine
        If .Range("E14").Value = "" Then msg = msg & "Enter City" & vbNewLine
        If .Range("C15").Value = "" Then msg = msg & "Select Country" & vbNewLine
        If .Range("D46").Value = "" Then msg = msg & "Enter contact information" & vbNewLine
        MsgBox msg, vbExclamation
        Cancel = True
    End If

    If WorksheetFunction.CountIf(.Range("C5"), "Modification") <> 0 Then
        If .Range("E5").Value = "" Then msg = msg & "blablablal...

..........same structure for other cases.......................................
...........................................................................................
...........................................................................................


    If Cancel Then
        MsgBox msg
    Else
           
    
End If
End With
End Sub

The thing is I continue receiving the MsgBox alert empty even if all the cells are filled in all the "cases" (Creation, modification, etc..) and it doesnt let me save the file.

I tried with an "ElseIf structure" without success, I suppose is something stupid

btw: i am using excel 2010

Regards,

m.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Maik,

Welcome to the forum - I hope you get as much help and assistance as you need.

Try this:

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
Dim msg As String
With Sheets("Form")
    msg=""
    If WorksheetFunction.CountIf(.Range("C5"), "Creation") <> 0 Then
        If .Range("C6").Value = "" Then msg = msg & "Select Customer Group" & vbNewLine
        If .Range("C7").Value = "" Then msg = msg & "Select Code" & vbNewLine
        If .Range("C11").Value = "" Then msg = msg & "Enter Company or Family Name" & vbNewLine
        If .Range("B12").Value = "First Name" And .Range("C12").Value = "" Then msg = msg & "Enter First Name" & vbNewLine
        If .Range("C13").Value = "" Then msg = msg & "Enter Address" & vbNewLine
        If .Range("C14").Value = "" Then msg = msg & "Enter Postal Code" & vbNewLine
        If .Range("E14").Value = "" Then msg = msg & "Enter City" & vbNewLine
        If .Range("C15").Value = "" Then msg = msg & "Select Country" & vbNewLine
        If .Range("D46").Value = "" Then msg = msg & "Enter contact information" & vbNewLine
        MsgBox msg, vbExclamation
        if msg<>"" then
              Cancel = True
        endif
    End If

    If WorksheetFunction.CountIf(.Range("C5"), "Modification") <> 0 Then
        If .Range("E5").Value = "" Then msg = msg & "blablablal...

..........same structure for other cases.......................................
...........................................................................................
...........................................................................................


    If Cancel Then
        MsgBox msg
    Else
           
    
End If
End With
End Sub

The changes are to ensure msg is blank at the start of the testing msg="" and then to only set Cancel = to true if there was a message if msg<>"" then Cancel = true endif

At present whenever C5 contains any of your key words Cancel is set to True and prevents you saving!

Hope this helps.

Regards
 
Upvote 0
Maik,

Does this help?

Code:
[FONT=monospace]Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
[/FONT]
msg = ""Dim msg As String
With Sheets("Form")    If WorksheetFunction.CountIf(.Range("C5"), "Creation") <> 0 Then        If .Range("C6").Value = "" Then msg = msg & "Select Customer Group" & vbNewLine        If .Range("C7").Value = "" Then msg = msg & "Select Code" & vbNewLine        If .Range("C11").Value = "" Then msg = msg & "Enter Company or Family Name" & vbNewLine        If .Range("B12").Value = "First Name" And .Range("C12").Value = "" Then msg = msg & "Enter First Name" & vbNewLine        If .Range("C13").Value = "" Then msg = msg & "Enter Address" & vbNewLine        If .Range("C14").Value = "" Then msg = msg & "Enter Postal Code" & vbNewLine        If .Range("E14").Value = "" Then msg = msg & "Enter City" & vbNewLine        If .Range("C15").Value = "" Then msg = msg & "Select Country" & vbNewLine        If .Range("D46").Value = "" Then msg = msg & "Enter contact information" & vbNewLine     
   If Not msg = "" Then MsgBox msg, vbExclamation        Cancel = True    End If    If WorksheetFunction.CountIf(.Range("C5"), "Modification") <> 0 Then        If .Range("E5").Value = "" Then msg = msg & "blablablal.............same structure for other cases.............................................................................................................................................................................................................................    If Cancel Then        If msg ="" Then MsgBox msg    Else               End IfEnd With [FONT=monospace]End Sub[/FONT]

Code is not easy to read!!??

Basically, at the start set msg = ""

Then for your two instances of display message use an if statement that checks if msg is still = ""
If it is it ignores Msg otherwise it displays.
 
Last edited:
Upvote 0
Hi Maik,

I think Tony and I have got similar approaches to solutions to the problem, though Tony's msg="" needs to come after the declaration of msg.

Tony, I have no idea why you're code is elongated either - I've had problems when the > and < symbols are interpreted as something to do with HTML, but cant see how that applies here!

Regards
 
Upvote 0
Peter,

Absolutely correct, case of fingers not connected to brain or brain operating at less than 100% or more likely, BOTH!! :)

Re the display of code. It was just a copy paste and small, albeit slightly flawed, edit of the original code. No < > symbols which as you say can confuse HTML if not separated by spaces.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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