VBA create worksheet if it doesn't exist

cazdealer

Board Regular
Joined
Mar 12, 2011
Messages
96
Hi,

how could I tell VBA to create a worksheet named "info" if it doesn't exist but skip that if that worksheet already exist in my workbook?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
Sub TEST()
    
    Dim i As Integer, blnFound As Boolean
    blnFound = False
    
    With ThisWorkbook
        For i = 1 To .Sheets.Count
            If .Sheets(i).Name = "Info" Then
                blnFound = True
                Exit For
            End If
        Next i
        
        If blnFound = False Then
            .Sheets.Add
            With ActiveSheet
                .Name = "Info"
            End With
        End If
    End With
    

End Sub
 
Upvote 0
I know this i quite old - but can someone please explain how with and this workbook works ?
 
Upvote 0
Hi,

how could I tell VBA to create a worksheet named "info" if it doesn't exist but skip that if that worksheet already exist in my workbook?

another way:

Code:
Sub CheckSheet()
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets("Info")
    If Err.Number = 9 Then
        Set ws = Worksheets.Add(After:=Sheets(Worksheets.Count))
        ws.Name = "Info"
    End If
    With ws
        'do stuff
    End With
End Sub

Dave
 
Upvote 0
The With ThisWorkbook like is matched with an End With line. Everything inbetween those lines is qualified to the ThisWorkbook object.

Code:
With ThisWorkbook
    For i = 1 To .Sheets.Count
        If .Sheets(i).Name = "Info" Then
            blnFound = True
            Exit For
        End If
    Next i
End With
is the same as


Code:
For i = 1 To ThisWorkbook.Sheets.Count
    If ThisWorkbook.Sheets(i).Name = "Info" Then
        blnFound = True
        Exit For
    End If
Next i
 
Upvote 0
LAST EDIT: Mike was faster
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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
Back
Top