VBA to call either of two macros based on whether workbook contains a specific sheet name

drewcarpenter2001

New Member
Joined
Dec 7, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have the following code to call either of two macros for each looped workbook based on whether the individual workbooks contain a tab called "Summary", which is an upstream system default for certain of the files. The logical expression for the first call does not seem to be functioning - it is bypassing and running the second macro for all workbooks. Help please!

Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Summary" Then
Call Finance_Close_Summary
Else
Call Finance_NonSummary
End If
Next ws
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
"For each" stop at 1st sheet only, then follow Call 1 or Call 2. from sheet2, never been looked into.
Try to set k as "counting variable" like this"
VBA Code:
For Each sh In ThisWorkbook.Sheets
    If sh.Name = "Summary" Then
    Call Finance_Close_Summary
    Exit Sub
    Else
    k = k + 1
    End If
Next
If k > 0 Then Call Finance_NonSummary
End Sub
Sub test()
 
Upvote 0
Thank you - it is still unable to correctly identify if a Summary tab exists and run the correct macro. I'm wondering at this point if the "real" name of the tab is the issue. I've shown below how it appears in the Objects library - you can see it is identified there as "Sheet7 (Summary)".

1638969853118.png
 
Upvote 0
You are not looping through any workbooks, you are only looking at the sheets in the workbook that contains the code.
Can you please post the rest of your code.
 
Upvote 0
VBA Code:
Sub LoopAllExcelFilesInFolder()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

  myExtension = "*.xls*"

  myFile = Dir(myPath & myExtension)

  Do While myFile <> ""
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
   
     Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
    If sh.Name = "Summary" Then
    Call Finance_Close_Summary
    Exit Sub
    Else
    k = k + 1
    End If
Next
If k > 0 Then Call Finance_NonSummary
     
      wb.Close SaveChanges:=True
     
      DoEvents

      myFile = Dir
  Loop

  MsgBox "Task Complete!"

ResetSettings:
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:
Upvote 0
Ok, try it like this
VBA Code:
   Dim sh As Worksheet
   Do While myFile <> ""
      Set wb = Workbooks.Open(FileName:=myPath & myFile)
      If ShtExists("Summary", wb) Then
         Call Finance_Close_Summary
      Else
         Call Finance_NonSummary
      End If
     
      wb.Close SaveChanges:=True
     
      DoEvents

      myFile = Dir
  Loop
and add this to a standard module
VBA Code:
Public Function ShtExists(ShtName As String, Optional Wbk As Workbook) As Boolean
    If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(Wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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