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
 
Code:
...
ws.Cells(iRow, 1).Value = Me.txtFile.Value
    ws.Cells(iRow, 2).Value = Me.txtDate.Value
    ws.Cells(iRow, 3).Value = Me.txtName.Value
   [B] [COLOR="Blue"]If[/COLOR] SheetExists(txtName.Text) [COLOR="Blue"]Then[/COLOR]
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR][/B]
    [COLOR="Blue"]Set[/COLOR] ws = Worksheets(Me.txtName.Value)
    ws.Cells(iRow, 1).Value = Me.txtFile.Value
    ws.Cells(iRow, 2).Value
...
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Compile error
Sub or Function not defined

I see that SheetExists is not defined, where can I define it by looking at my code?

Thank you.
 
Upvote 0
I know, my issue is there.
Should I put the Function above "Private Sub cmdAdd_Click()"?

Function WorksheetExists(SheetName As String) As Boolean
Private Sub cmdAdd_Click()

I would appreciate if you may integrate SheetsExists function in my code and provide it to me so I will have a better understanding.

many thanks.
 
Upvote 0
Just put this function into standard module.
 
Upvote 0
Compile error
Expected End Sub

What I am trying is, it will create the sheet if it doesn't exist and if it exists, it will update it.

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
Function SheetExists(SheetName As String) As Boolean
    Dim sh As Worksheet
    On Error Resume Next
    Set sh = Worksheets(sh)
    If Not sh Is Nothing Then SheetExists = True
End Function
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
Create some module (Insert -> Module) and place function there.
 
Upvote 0
If I create a module and insert SheetsExists and keep my code as it is, I get the error below when I use my UserForm for the second time.

Run-time error '1004':
Canot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic.
 
Upvote 0
Ah, my bad... Add Not:
Code:
If [B][COLOR="Red"]Not[/COLOR][/B] SheetExists(txtName.Text) Then
...
 
Upvote 0
I get the same error

Run-time error '1004':
Canot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic.

Can you please provide me the entire code I need to use?
Thank you.<!-- / message -->
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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