Check / Update Worksheet

Cisco7970

New Member
Joined
Jul 11, 2011
Messages
15
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 and it can sucessfully create a dynamic sheet but it doesn't have the functionality to check and update if the file exists. It always tries to crate the same sheet and giving an error.
I would appreciate if you may help me.

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

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.
You have posted this already, please don't duplicate posts (although you have used a different thread name).

You can always bump to see if someone can help you.

In the previous threads you were advised how to deal with this.

The function needs to go outside this code. You basically will call the function within the code from your form button.

If you can't get it to work then send a message to me, with your email address.
 
Upvote 0
Ok I will see what I can do to help you find a solution.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,135
Members
452,890
Latest member
Nikhil Ramesh

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