ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Test to see if a worksheet exists thanks, Chip (https://www.excelbanter.com/excel-programming/275013-re-test-see-if-worksheet-exists-thanks-chip.html)

CT[_2_]

Test to see if a worksheet exists thanks, Chip
 
Thanks, Chip
Chip Pearson wrote in message
...
Try something like the following;

Function WorksheetExists(SheetName As String, _
Optional WhichBook As Workbook) As Boolean
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = Len(WB.Worksheets(SheetName).Name) 0
End Function

You can then call this function in code as follows:

If WorksheetExists("Sheet123") = True Then
' sheet exists
Else
' sheet does not exist
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"CT" wrote in message
ink.net...
Sometimes when I try to add a new sheet ...
Worksheets.Add.Move after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Input"
I fail because there is already a worksheet named "Input".

So, before adding the new sheet, I do ...
For Each MyWorksheet In Worksheets
If MyWorksheet.Name = "Input" Then
Application.DisplayAlerts = False
Worksheets("Input").Delete
Application.DisplayAlerts = True
End If
Next MyWorksheet

Is there a more simple way to check to see if the "Input" sheet

exists?








All times are GMT +1. The time now is 09:47 AM.

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