Check if sheet exists, create it if it doesn't

ossuary

Active Member
Joined
Sep 5, 2004
Messages
279
Hello gurus,

Quick question... Can someone please tell me how to use VBA to check whether or not a worksheet named "test" exists in a workbook, and if it does not, create it?

Thanks in advance. :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi

Perhaps:

Code:
  On Error Resume Next
  Set sht = Worksheets("Test")
  If sht Is Nothing Then Worksheets.Add:ActiveSheet.Name = "Test"
  On Error Goto 0
 
Upvote 0

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
Code:
    On Error Resume Next
        If Not Worksheets("Test").Name = "Test" Then Worksheets.Add.Name = "Test"
    On Error GoTo 0
 
Upvote 0

ossuary

Active Member
Joined
Sep 5, 2004
Messages
279
Thanks for the reply.

I need to do it without any possibility of an error pass. I have code to search whether or not a file exists:

Code:
        With Application.FileSearch
            .NewSearch
            .LookIn = ThisWorkbook.Path & "\Core Files\Employee Journals"
            .SearchSubFolders = False
            .Filename = OpenedFile
            .MatchTextExactly = True
            .FileType = msoFileTypeExcelWorkbooks
        If .Execute() = 1 Then

I was hoping to have something similar to check the worksheets, rather than using an error check to bypass the sheet creation. I'm just not sure what the syntax would be. Is there another way to do this?

Also, is there a way to specify what name an added worksheet will have, or do you have to have it be the active worksheet in order to rename it? The problem is that I will have multiple workbooks open when this code is executed, and it is possible that the workbook that is being modified will not be the active workbook, which could cause errors.
 
Upvote 0

ossuary

Active Member
Joined
Sep 5, 2004
Messages
279
Code:
    On Error Resume Next
        If Not Worksheets("Test").Name = "Test" Then Worksheets.Add.Name = "Test"
    On Error GoTo 0

xld,

I'll give this a try and see if it works for what I'm trying to do. Thanks. :)
 
Upvote 0

Forum statistics

Threads
1,190,881
Messages
5,983,383
Members
439,841
Latest member
goodwillhunting

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
Top