Help with vbYes

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,215,477
Messages
6,125,037
Members
449,205
Latest member
Eggy66

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