Run Macro if all sheets are in the workbook

vinzent

New Member
Joined
Feb 7, 2012
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello to all

I have one macro, but I want to check if all the sheets in the file are in the workbook before to run (due we are using 10 different sheets with different names to get the information) and sometimes one sheet is missing to included before to run and only show error but we are checking on by one to know what is the missing sheet.
If yes, run the macro normally, If not show a message with sheet name missing and stop the macro.
Thanks a lot..
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Obviously to detect a missing sheet, you have to know what sheets you are expecting. This code set up a list of expected sheet names and puts up a message for each one not found. You can insert this code in the desired place in existing code. Note that it takes into account that more than one sheet could be missing.

VBA Code:
   Dim SN As Variant
   Dim SNC As String
   Dim Missing As Boolean

   On Error Resume Next
   For Each SN In Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")
   
      SNC = Worksheets(SN).Name
      If Err.Number > 0 Then
         MsgBox "Sheet """ & SN & """ missing."
         Missing = True
         Err.Clear
      End If
      
   Next SN
   
   If Missing Then Exit Sub

   On Error GoTo 0
 
Upvote 0
Solution
Obviously to detect a missing sheet, you have to know what sheets you are expecting. This code set up a list of expected sheet names and puts up a message for each one not found. You can insert this code in the desired place in existing code. Note that it takes into account that more than one sheet could be missing.
I included in my macro and run ok, this is just I need it. Thanks a lot.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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