MsgBox to display results upon closing

greatcookies

Board Regular
Joined
Sep 25, 2005
Messages
187
How can I have a message box display the results of a formula upon closing the workbook. I need to display the number of cells in a particular column less than 0. I assume you would use the countif formula (=countif(e4:e35,">0") I am going to either have display upon closing or upon hitting a command button on the sheet.

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
You're right on the money:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_Open()
    MsgBox Application.WorksheetFunction.CountIf(Range("E4:E35"), "<0")
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope that helps,

Smitty

EDIT: I forgot the close event:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeClose(Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)
    MsgBox Application.WorksheetFunction.CountIf(Range("E4:E34"), "<0")
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
How can I have the msgbox also say sonething like " Number of invalid counts"?

Here you go:

<font face=Courier New>    MsgBox "There are " & _
        Application.WorksheetFunction.CountIf(Range("E4:E35"), "<0") & _
        " Invalid Counts", vbOKOnly + vbInformation, "Invalid Counts"</FONT>

Where the syntax is MsgBox Primary Message, Buttons & Caption, Message Title.

Smitty
 
Upvote 0

Forum statistics

Threads
1,191,708
Messages
5,988,232
Members
440,139
Latest member
ngaicuong2017

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
Top