How to check if a worksheet exists in a workbook?l

T

Tom McEwen

Does anyone know a simple way to check to see if a Worksheet with a specific
name already exists in an open Workbook? This is to check before trying to
add a new Worksheet with that same name.'

I know how to retrieve a list of existing Worksheets and could just loop
through these to check, but figure there must be a more elegant way.
 
C

Cindy M.

Hi Tom,
Does anyone know a simple way to check to see if a Worksheet with a specific
name already exists in an open Workbook? This is to check before trying to
add a new Worksheet with that same name.'

I know how to retrieve a list of existing Worksheets and could just loop
through these to check, but figure there must be a more elegant way.
For VBA probably not. If this is a .NET language you could try:
Dim oWorksheet as Excel.Worksheet = oWorkbook.Sheets("the name")

In the .NET Framework oWorksheet will be Nothing (null in C#), so then you test
that:
If Is Nothing oWorksheet Then
'You can insert the new sheet

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
S

Steve Rindsberg

The same approach would work in VBA as well, no?

Sub test()

Dim oWorkSheet As Excel.Worksheet

On Error Resume Next

Set oWorkSheet = ActiveWorkbook.Worksheets("SomeName")
If oWorkSheet Is Nothing Then
MsgBox "No such worksheet"
Else
MsgBox "Found It"
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top