Macro to identify if work sheets exist then run seperate macros if it does and contine if not

paul_sykes00

Board Regular
Joined
May 7, 2012
Messages
53
Hi,

I am looking for a macro that will do the follow:
1) Check if "sheet x" exists
2) If does run an edit macro I have create (i.e. macro 1)
3) Then check if "sheet y" exists
4) If does run a 2nd edit macro I have create (i.e. macro 2)
5) End

Hope that makes sense. Look forward to your responses,

Thanks

Paul
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try
Code:
Sub tester()
Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name = "sheet x" Then Call macro1
        If ws.Name = "sheet y" Then Call macro2
Next ws
End Sub
 
Upvote 0
Thanks Michael,

If the workbook has say 100 sheets is there a way to make the macro check all sheets to see if "sheet x" or/and "sheet y" exist?
 
Upvote 0
The posted code does that !!
Have you tried it ?
It looks at each sheet name and if it's neither name then goes to the next sheet until it either finds the named sheet or finds nothing.
Doesn't matter how many sheets there are.
 
Upvote 0
Hi all,

Another approach is to employ a function. This avoids stepping through each sheet.

Code:
Sub tester2()
    If SheetExists("sheet x") Then Call Macro1
    If SheetExists("sheet y") Then Call Macro2
End Sub

Function SheetExists(sName As String) As Boolean
    On Error Resume Next
    SheetExists = Sheets(sName).Index > 0
End Function
 
Upvote 0
Thanks guys, both methods worked well. The reason i was having difficulties at first was because I forgot to make the sheet I wanted to edit active at the start of Macro1,Macro2 etc
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,770
Members
449,049
Latest member
greyangel23

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