Check if Worksheet exists

Cisco7970

New Member
Joined
Jul 11, 2011
Messages
15
Hi,

I am creating a worksheet in VBA, I would like to create an intelligence, if I run the macro again, it will check if the worksheet exists already, if it exists it will update the existing one. If it doesn't exist, it will create it.
At the moment, with the code below, I am creating my worksheet

Code:
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName

txtName is a field which user fills in and Worksheet is created with that name. How can I achieve this?
Thank you.

Cisco 7970
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
The following function should do the trick:
Rich (BB code):
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean 

On Error Resume Next
WorksheetExists = (Sheets(WorksheetName).Name <> "")
On Error GoTo 0

End Function
 
Upvote 0
But where do you create the Worksheet as in my example?

Code:
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName

Can you please adjust the code with this?
Thank you
 
Upvote 0
This is how you use the function within your code:
Code:
If WorksheetExists(txtName) = False Then
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName
End If
Sheets(txtName).Activate
The code checks if the worksheet exists and creates it if needed. Then it activates the sheet called txtName.
 
Upvote 0
In your first post you said txtName is filled in by the user. You need to give it a value before you can use it.
 
Upvote 0
Here's simple stand alone code:
Code:
Sub AddSheetIfNeeded()
Dim txtName As String
txtName = InputBox("Type the sheet name", "Sheet Name ?")
If WorksheetExists(txtName) = False Then
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName
End If
Sheets(txtName).Activate
End Sub
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean
On Error Resume Next
WorksheetExists = (Sheets(WorksheetName).Name <> "")
On Error GoTo 0
End Function
 
Upvote 0
Code:
[COLOR="Blue"]Function[/COLOR] SheetExists(SheetName [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Boolean[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] sh [COLOR="Blue"]As[/COLOR] Worksheet
    [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]Resume[/COLOR] [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]Set[/COLOR] sh = Worksheets(sh)
    [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] sh [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR] SheetExists = [COLOR="Blue"]True[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0
Apologizes,
I still struggle with this where to put the condition, below is my code and user fills a userform where txtName is defined. The UserForm feeds the Main sheet and also a new sheet is created with the name of txtName and also some values are copied to the new sheet.

Code:
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Main")

iRow = ws.Cells(Rows.Count, 1) _
  .End(xlUp).Offset(1, 0).Row

ws.Cells(iRow, 1).Value = Me.txtFile.Value
ws.Cells(iRow, 2).Value = Me.txtDate.Value
ws.Cells(iRow, 3).Value = Me.txtName.Value
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName
Set ws = Worksheets(Me.txtName.Value)
ws.Cells(iRow, 1).Value = Me.txtFile.Value
ws.Cells(iRow, 2).Value = Me.txtDate.Value
ws.Range("A1:C1") = Array("File", "Date")

Me.txtFile.Value = ""
Me.txtDate.Value = ""
Me.txtChauffeur.Value = ""
Me.txtFile.SetFocus
End Sub
Private Sub cmdClose_Click()
  Unload Me
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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