Help with vbYes

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Code:
Sub isAnyWorkbookOpen()    
    Dim wb As Workbook
    Dim wbs As Workbooks
    Dim msg As String
    Dim answer As String
    
    Set wbs = Application.Workbooks
    
    msg = "The following worksbooks must be closed before continuing." & Chr(10) & "Do you want to save and close these workbooks?" & Chr(10) & Chr(10)
    
    For Each wb In wbs
        If wb.Name <> ThisWorkbook.Name Then msg = msg & wb.Name & Chr(10)
    Next wb
    
    MsgBox (msg), 52, "Workbooks Open"
        If answer = vbYes Then Call closeOtherWorkbooks
        
End Sub
When I Dim answer as String, I receive a Runtime error 13: Type Mismatch. Which I understand. However when I change the Dim answer as Boolean or Variant, the MsgBox will be displayed as expected but skips over
Code:
If answer = vbYes Then Call closeOtherWorkbooks
When I use the Watch window it states the value of Expression answer is Empty.
What am I doing wrong?

Thank You
 
Last edited:
Are you trying to check if any other workbooks, apart from the one the code is in, are open?
Yes.
What do you want to happen if other workbooks are open?
Run the following code:
Code:
If wb.Name <> ThisWorkbook.Name Then msg = msg & wb.Name & Chr(10)
ans = MsgBox(msg, 52, "Workbooks Open")

Thank You for your help.
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Those are 2 quite separate lines of code, the first is within the loop and the second is after it.

Do you want to build up a list with the names of any workbooks that are open other than the one the code is in?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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