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