If sheet exists then run code

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hi All,

Im trying to check if the sheet exists and then run code if it does and if not skip that code and check for the next sheet.

If SheetExists("IProcessed") Then

code
else

If SheetExists("IProcessed") Then
code

its not working for some reason. I think im missing something easy. What are your throghts?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Have you got a function called SheetExists?
 
Upvote 0
no I had a function that I copied from a website but deleted it because for whatever reason it did not work. I have found many solutions, but cant seem to implement them for my situation.
 
Upvote 0
Try the code below if the sheet is in the same workbook

Rich (BB code):
Sub shtext2()
    If Evaluate("ISREF('IProcessed'!A1)") Then   
       'code to run if sheet exists
    Else
        'code to run if sheet doesn't exists
    End If
End Sub
 
Upvote 0
Try the code below if the sheet is in the same workbook

Rich (BB code):
Sub shtext2()
    If Evaluate("ISREF('IProcessed'!A1)") Then  
       'code to run if sheet exists
    Else
        'code to run if sheet doesn't exists
    End If
End Sub
seems like it works thank you! now im getting a block if error basically theres no end if .
If Evaluate("ISREF('IProcessed'!A1)") Then
'code to run if sheet exists
Else
If Evaluate("ISREF('DCS'!A1)") Then
'code to run if sheet doesn't exists
else
If Evaluate("ISREF('TYT'!A1)") Then
code


End If

do I have to put and end if on every if statement? im using it for many different tabs with different names and different code
 
Upvote 0
You need to use ElseIf
VBA Code:
    If Evaluate("ISREF('IProcessed'!A1)") Then
        MsgBox "1"
    ElseIf Evaluate("ISREF('DCS'!A1)") Then
        MsgBox "2"
    ElseIf Evaluate("ISREF('TYT'!A1)") Then
        MsgBox "3"
    Else
        MsgBox "NO SHEETS FOUND"
    End If
 
Upvote 0
Solution
You need to use ElseIf
VBA Code:
    If Evaluate("ISREF('IProcessed'!A1)") Then
        MsgBox "1"
    ElseIf Evaluate("ISREF('DCS'!A1)") Then
        MsgBox "2"
    ElseIf Evaluate("ISREF('TYT'!A1)") Then
        MsgBox "3"
    Else
        MsgBox "NO SHEETS FOUND"
    End If
worked thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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