need vba code to have message appear based on outcome

3earth

New Member
Joined
Mar 21, 2010
Messages
17
I have A11:G11 summed in cell H11. If H11 equals 0 (zero), I would like a message to pop up "You must select at least ONE location." I would like this message to pop up before close/save of the file. running excel 2007
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Not tested it but:


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

[COLOR=#333333]Application.EnableEvents = False[/COLOR]
[COLOR=#333333]Set myRng = Range("H11") ' [/COLOR]
[COLOR=#333333]If Application.WorksheetFunction.Sum(myRng) = 0 Then[/COLOR]
[COLOR=#333333]MsgBox "[/COLOR][COLOR=#574123]You must select at least ONE location.[/COLOR][COLOR=#333333]"[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Set myRng = Nothing[/COLOR]
[COLOR=#333333]Application.EnableEvents = True[/COLOR]

End Sub
 
Upvote 0
Hi,

Try on a copy of sheet.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets("Sheet1").Range("H11") = 0 Then
    MsgBox "You must select at least ONE location."
End If
End Sub

Jai
 
Upvote 0
Jai - I was not able to get this to work. Not sure what I am doing wrong. Do you have to enter the data in a certain order?
 
Upvote 0
Ghris - This works but after you hit OK in the message box, the document can be closed. How do I stop the close?
 
Upvote 0
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

[COLOR=#333333]Application.EnableEvents = False[/COLOR]
[COLOR=#333333]Set myRng = Range("H11") ' [/COLOR]
[COLOR=#333333]If Application.WorksheetFunction.Sum(myRng) = 0 Then[/COLOR]
[COLOR=#333333]MsgBox "[/COLOR][COLOR=#574123]You must select at least ONE location.[/COLOR][COLOR=#333333]"[/COLOR]
    If MsgBox("Click cancel to select a location", 36, "Confirm") = vbNo Then
        Cancel = True
    End If
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Set myRng = Nothing[/COLOR]
[COLOR=#333333]Application.EnableEvents = True[/COLOR]

End Sub

Not sure if this is going to work, but let me know :)

Edit:
Oh yes, this works I believe.
 
Last edited:
Upvote 0
The confirm shows up with Yes and No options. No = back to sheet (to hopefully have someone select a location). Yes = document closes. Almost. Is there anything else that can be done?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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