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:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Maybe this way
Code:
Sub isAnyWorkbookOpen()
Dim wb As Workbook, wbs As Workbooks, msg As String
Set wbs = Application.Workbooks
For Each wb In wbs
    If wb.Name <> ThisWorkbook.Name Then msg = msg & wb.Name & Chr(10)
Next wb
msg = "The following worksbooks must be closed before continuing." & Chr(10) & "Do you want to save and close these workbooks?" & Chr(10) & Chr(10)
MsgBox (msg), 52, "Workbooks Open"
    If vbYes = 6 Then Call closeOtherWorkbooks
End Sub
 
Upvote 0
The value of the constant vbYes is 6 so the expression in this If statement will always evaluate to True and closeOtherWorkbooks will always be called regardless of the response to the message box.
Code:
    If vbYes = 6 Then Call closeOtherWorkbooks
Is that what you want to happen?

If you want the code to only call closeOtherWorkbooks when the user clicks Yes on the message box try this.
Code:
Sub isAnyWorkbookOpen()
Dim wb As Workbook, wbs As Workbooks, msg As String
Dim Ans As Long

    Set wbs = Application.Workbooks
    For Each wb In wbs
        If wb.Name <> ThisWorkbook.Name Then msg = msg & wb.Name & Chr(10)
    Next wb
    msg = "The following worksbooks must be closed before continuing." & Chr(10) & "Do you want to save and close these workbooks?" & Chr(10) & Chr(10)

    Ans = MsgBox(msg, 52, "Workbooks Open")
    
    If Ans = vbYes Then
        Call closeOtherWorkbooks
    End If
    
End Sub
 
Upvote 0
Another option
Code:
If MsgBox(Msg, 52, "Workbooks Open") = vbYes Then Call closeOtherWorkbooks
 
Upvote 0
Good point Norie, the vbMsgBoxStyle value = 52 which includes the Yes and No buttons.So if the user clicks the No button, I probably need to do something like exit sub or just exit. So I changed the code a bit from what I originally posted. Not sure if I need to post this in a new thread. Someone please let me know before I post new code. Don't want to get in trouble with this forum. Thank you.
 
Upvote 0
What do you want to happen if the user clicks No?

In the code I posted if the user clicks No then no further action will be taken and the code will end 'naturally'.
 
Upvote 0
Code:
Sub isAnyWorkbookOpen()    
    Dim wb As Workbook
    Dim wbs As Workbooks
    Dim msg As String
    Dim ans As Long
    
    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)
        Else
            Exit Sub
        End If
    Next wb
    
   ans = MsgBox(msg, 52, "Workbooks Open")
        If ans = vbYes Then
            Call closeOtherWorkbooks
        Else
            End
        End If
End Sub
What I am wanting this code to do, is first check what is already open, if those workbook names doesn't equal ThisWorkbook name then run the following code
Code:
msg = msg & wb.Name & Chr(10)
but if there are no other workbooks open other than ThisWorkbook and it is equal to wb.name then Exit Sub.
However the why this code is phrased as soon as the first line of the If statement is checked it jumps to the Else statement which makes sense. So how can I rephrase this to do what I want. Thank you.
 
Last edited:
Upvote 0
What, in words, are you trying to do?

Are you trying to check if any other workbooks, apart from the one the code is in, are open?

What do you want to happen if other workbooks are open?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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