Hello, I am wondering how to write a code that checks the number of workbooks open and, if it is more that one, pops up a message box giving the option to exit the macro by pressing no or cancel. I have successfully gotten the message box to show but cannot get the additional NO button to work correctly. Thank you!
Last edited by kirsty; 12-12-2010 at 02:05 AM.
Hi kirsty
hard to say with out code but maybe...Sub CountWorkbooks() Dim Xresponse As Integer Xresponse = MsgBox("More that one workbook open." & Chr(10) & "Workbooks Open =" & Windows.Count, vbYesNo + vbCritical) Select Case Xresponse Case 6 MsgBox ("Yes") Case 7 MsgBox ("No") End Select End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Thank you very much pike. This works to get both Yes and No buttons to show however neither response exits the macro on my computer? If I answer "No" I need the macro to exit. Do you know how to modify the code to do that?
Hi kirsty
Sub CountWorkbooks() Dim Xresponse As Integer Xresponse = MsgBox("More that one workbook open." & Chr(10) & "Workbooks Open =" & Windows.Count, vbYesNo + vbCritical) Select Case Xresponse Case 6 MsgBox ("Yes") Case 7 MsgBox ("No") Exit Sub ' This line will exit the sub End Select End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Hey, thank you very much. I was able to tweak it to do what I needed. Have a nice weekend!
FYI, all....
Since you only really need to "do" something specific if one answer is given, you can slim that down to one command:
Sub CountWorkbooks() If MsgBox("More that one workbook open." & vbLf & "Workbooks Open = " & Windows.Count _ & vbLf & vbLf & "Do you wish to abort?", vbYesNo + vbCritical) = vbYes Then Exit Sub End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks