Check if Sheet exists, run macro if it does, if not then continue rest of code.

dman78

New Member
Joined
Oct 22, 2016
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hello and thanks in advance.

Quick question, I hope!

I have code that I want to check if a sheet exists, and if it exists a sub macro is run. But if there is no worksheet with that name then I want to continue the code.

I am having trouble with the part where if there is no worksheet to let the code run as it was running before this check.

VBA Code:
 Check to see if "CC Date" sheet exists; if yes then run Clean_Poes_Data

    For Each ws In Worksheets
        If ws.Name = "CC Date" Then Call Clean_POES_DATA
        Next
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Rather than looping through the sheets, you can use
VBA Code:
If Evaluate("isref('CC DATE'!A1)") Then Call Clean_POES_DATA
 
Upvote 0
Solution
Alternatively

VBA Code:
    'Check to see if "CC Date" sheet exists; if yes then run Clean_Poes_Data
    For Each ws In Worksheets
        If ws.Name = "CC Date" Then
            Clean_POES_DATA
            Exit For
        End If
    Next ws
 
Upvote 0
Alternatively

VBA Code:
    'Check to see if "CC Date" sheet exists; if yes then run Clean_Poes_Data
    For Each ws In Worksheets
        If ws.Name = "CC Date" Then
            Clean_POES_DATA
            Exit For
        End If
    Next ws
Rather than looping through the sheets, you can use
VBA Code:
If Evaluate("isref('CC DATE'!A1)") Then Call Clean_POES_DATA

Thanks to you both! That was fast.
 
Upvote 0
Does that solve your problem?
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,726
Members
449,465
Latest member
TAKLAM

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