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
 
Can someone please help me?
Below is my code. I have a userform which gets File, Date and Name from the user and writes this to the Main sheet. Also at the same, it creates a new sheet with the same name as Name field. If there is an existing Sheet, it will update the existing one and if this is a new sheet, it will create it. My code is below which is working fine sucessfully can create a dynamic sheet but it doesn't have the functionality to check and update if there is an existing version.
Thank you.

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
[COLOR=red]Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtName
[/COLOR]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

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

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