MsgBox if multiple Workbooks with similar name are open

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
In the code below I have a MsgBox if a particular Workbook isn't open, and I would like to add another MsgBox if more than one Workbook is open that fits the criteria.

VBA Code:
Sub set_transcript_wbk()
    Dim MyWkb As Workbook
    Dim wkb As Workbook
    
    For Each wkb In Workbooks
        If wkb.Name Like "Transcript*.xls" Then
            Set MyWkb = Workbooks(wkb.Name)
            Exit For
        End If
    Next wkb
    
    If MyWkb Is Nothing Then
        MsgBox "Please open your Transcript file", vbExclamation
    End If
    
End Sub

My guess is adding something like
VBA Code:
    If Not MyWkb Is Nothing Then
        MsgBox "Please close all but one Transcript file and re-run macro", vbExclamation
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How about
VBA Code:
Sub set_transcript_wbk()
    Dim MyWkb As Workbook
    Dim wkb As Workbook
    Dim i As Long
    
    For Each wkb In Workbooks
        If wkb.Name Like "Transcript*.xls" Then
            Set MyWkb = Workbooks(wkb.Name)
            i = i + 1
        End If
    Next wkb
    
    If i > 1 Then
      Set MyWkb = Nothing
      MsgBox "Please close all but one Transcript file and re-run macro", vbExclamation
      Exit Sub
   End If
    If MyWkb Is Nothing Then
        MsgBox "Please open your Transcript file", vbExclamation
    End If
    
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub set_transcript_wbk()
    Dim MyWkb As Workbook
    Dim wkb As Workbook
    Dim i As Long
   
    For Each wkb In Workbooks
        If wkb.Name Like "Transcript*.xls" Then
            Set MyWkb = Workbooks(wkb.Name)
            i = i + 1
        End If
    Next wkb
   
    If i > 1 Then
      Set MyWkb = Nothing
      MsgBox "Please close all but one Transcript file and re-run macro", vbExclamation
      Exit Sub
   End If
    If MyWkb Is Nothing Then
        MsgBox "Please open your Transcript file", vbExclamation
    End If
   
End Sub
Oh, I like that. Great idea.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

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