ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If Worksheet exists...Then run code (https://www.excelbanter.com/excel-programming/353650-if-worksheet-exists-then-run-code.html)

PCLIVE

If Worksheet exists...Then run code
 
I'm looking for some code that will allow me to search for a specific
worksheet.

If it doesn't exist, then run some code.
The code below obviously doesn't work. How can I rewrite this?


If Sheets(CurrentModel).Activate = Error Then
'run code'

Else
Sheets(CurrentModel).Activate
End If


Thanks,
Paul



dok112[_88_]

If Worksheet exists...Then run code
 

Put this where you want the code. This will loop through all the sheet
names looking for the name you specify.

For each WS in worksheets
if WS.name = "sheetname" 'sheet to be found
'code to be run
else:
'alternate code to be run
end if
next WS


--
dok112
------------------------------------------------------------------------
dok112's Profile: http://www.excelforum.com/member.php...o&userid=10581
View this thread: http://www.excelforum.com/showthread...hreadid=513809


GS

If Worksheet exists...Then run code
 
try this:

Function bSheetExists(wksName As String) As Boolean
' Checks if a specified worksheet exists.
'
' Arguments: wksName The name of the worksheet
'
' Returns: TRUE if the sheet exists

Dim x As Worksheet
On Error Resume Next
Set x = ActiveWorkbook.Sheets(wksName)
bSheetExists = (Err = 0)

End Function

To use:
If bSheetExists("NameOfSheet") Then...
-OR-
If Not bSheetExists("NameOfSheet") Then...

Hope this helps!
GS

PCLIVE

If Worksheet exists...Then run code
 
Sorry dok112,

I guess I worded this slightly incorrect. What I really need is to run code
if the sheet does NOT exist. I'm not sure this is possible with this type
of code.

I'll keep trying.
Thanks.

"dok112" wrote in
message ...

Put this where you want the code. This will loop through all the sheet
names looking for the name you specify.

For each WS in worksheets
if WS.name = "sheetname" 'sheet to be found
'code to be run
else:
'alternate code to be run
end if
next WS


--
dok112
------------------------------------------------------------------------
dok112's Profile:
http://www.excelforum.com/member.php...o&userid=10581
View this thread: http://www.excelforum.com/showthread...hreadid=513809




dok112[_90_]

If Worksheet exists...Then run code
 

It will work. You would just need to reword it differently.

For each WS in worksheets
if WS.name = "sheetname" 'sheet to be found
else:
'code to be run
end if
next WS

With it this way, it's going to look for the sheetname. If it finds
it, it will skip that sheet. If it doesnt find it, then it will run
the code specified.

For example, if your sheetname is "Sheet5", if sheet5 is not there,
then it's going to run the code you specified. If it does exist, then
it will just continue to loop until it hits the end of the sheets.


--
dok112
------------------------------------------------------------------------
dok112's Profile: http://www.excelforum.com/member.php...o&userid=10581
View this thread: http://www.excelforum.com/showthread...hreadid=513809



All times are GMT +1. The time now is 11:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com