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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,151
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

paul_sykes00

Board Regular
Joined
May 7, 2012
Messages
53
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?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,151
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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.
 

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787

ADVERTISEMENT

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
 

paul_sykes00

Board Regular
Joined
May 7, 2012
Messages
53
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,151
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
OK, glad you got it sorted....(y)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,752
Messages
5,638,162
Members
417,011
Latest member
Amaden95

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
Top