Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If WorkSheet("wsName") Exists [duplicate]

Tags:

excel

vba

I'm wondering if there is clean cut functionality that returns True or False if a worksheet inside a workbook exists?

It would be good, but not essential, if it's possible to do it without skipping error handling.

The only thing I've found doesn't really work:

On Error Resume Next If (Worksheets("wsName").Name <> "") Then     Debug.Print "Worksheet exists!" Else     Debug.Print "Worksheet doesn't exist!" End If On Error GoTo ErrHandler 
like image 282
Matt Rowles Avatar asked May 18 '11 05:05

Matt Rowles


People also ask

How do I check if a worksheet exists?

What is this? With this code we can use =WorksheetExists(B3) to test any text string to see if it exists as a sheet name in the current workbook.

How do I find duplicate sheets in Excel?

Select the cells you want to check for duplicates. Note: Excel can't highlight duplicates in the Values area of a PivotTable report. Click Home > Conditional Formatting > Highlight Cells Rules > Duplicate Values.

How do you duplicate data in a worksheet?

Keyboard shortcut: Press CTRL+Spacebar, on the keyboard, and then press Shift+Spacebar. Copy all the data on the sheet by pressing CTRL+C. Click the plus sign to add a new blank worksheet. Click the first cell in the new sheet and press CTRL+V to paste the data.


Video Answer


2 Answers

A version without error-handling:

Function sheetExists(sheetToFind As String) As Boolean     sheetExists = False     For Each sheet In Worksheets         If sheetToFind = sheet.name Then             sheetExists = True             Exit Function         End If     Next sheet End Function 
like image 100
Dante May Code Avatar answered Sep 19 '22 21:09

Dante May Code


There's no built-in function for this.

Function SheetExists(SheetName As String, Optional wb As Excel.Workbook)    Dim s As Excel.Worksheet    If wb Is Nothing Then Set wb = ThisWorkbook    On Error Resume Next    Set s = wb.Sheets(SheetName)    On Error GoTo 0    SheetExists = Not s Is Nothing End Function 
like image 21
Tim Williams Avatar answered Sep 19 '22 21:09

Tim Williams