Hi Guys,
I need help please dealing with errors when creating new sheets from a template.
Objective is:
Within a sheet named CUSTOMERS:
1. Enter an Invoice Number in Cell G3.
2. Click a Forms Button to run Sub "CreateInvoice".
Sub "CreateInvoice" should:
1. Copy a TEMPLATE sheet and place this copy after the CUSTOMERS sheet.
2. Name the new copy Sheet Tab with the contents of CUSTOMERS sheet cell G3.
3. Clear the contents of CUSTOMERS sheet cell G3.
4. Display the newly created sheet ready to use (presume by placing cursor in cell X of the new sheet?).
Plus a nice to have would be to react to an "Error" in a meaningful way.
By that I mean do not create duplicate sheet names (ie: "xxxxx(2)") but bring up a prompt to warn the user.
The following code is basically working in that it creates a new sheet with the cell G3 name.
But the whole error trapping bit is in its n'th revision and I am going round in ever decreasing circles!
"Sub CreateInvoice_Click()
'Copies the TEMPLATE sheet and names it with the Invoice Number in cell G3 of CUSTOMERS sheet
Dim MySheetName As String
Sheets("TEMPLATE").Copy After:=Sheets("CUSTOMERS")
ActiveSheet.Name = Sheets("CUSTOMERS").Range("G3")
On Error GoTo Err_Trap
ActiveSheet.Name = MySheetName
Err_Trap:
If Err.Number = 1004 Then
Application.DisplayAlerts = False
Sheets("TEMPLATE(2)").Delete
Application.DisplayAlerts = True
MsgBox ("NEW INVOICE NOT CREATED." & Chr(10) & _
"Invoice Already Exists."), vbInformation, "Sheet Creation Error"
Exit Sub
End If
End Sub"
Any help would be much appreciated.
Regards,
Mike
I need help please dealing with errors when creating new sheets from a template.
Objective is:
Within a sheet named CUSTOMERS:
1. Enter an Invoice Number in Cell G3.
2. Click a Forms Button to run Sub "CreateInvoice".
Sub "CreateInvoice" should:
1. Copy a TEMPLATE sheet and place this copy after the CUSTOMERS sheet.
2. Name the new copy Sheet Tab with the contents of CUSTOMERS sheet cell G3.
3. Clear the contents of CUSTOMERS sheet cell G3.
4. Display the newly created sheet ready to use (presume by placing cursor in cell X of the new sheet?).
Plus a nice to have would be to react to an "Error" in a meaningful way.
By that I mean do not create duplicate sheet names (ie: "xxxxx(2)") but bring up a prompt to warn the user.
The following code is basically working in that it creates a new sheet with the cell G3 name.
But the whole error trapping bit is in its n'th revision and I am going round in ever decreasing circles!
"Sub CreateInvoice_Click()
'Copies the TEMPLATE sheet and names it with the Invoice Number in cell G3 of CUSTOMERS sheet
Dim MySheetName As String
Sheets("TEMPLATE").Copy After:=Sheets("CUSTOMERS")
ActiveSheet.Name = Sheets("CUSTOMERS").Range("G3")
On Error GoTo Err_Trap
ActiveSheet.Name = MySheetName
Err_Trap:
If Err.Number = 1004 Then
Application.DisplayAlerts = False
Sheets("TEMPLATE(2)").Delete
Application.DisplayAlerts = True
MsgBox ("NEW INVOICE NOT CREATED." & Chr(10) & _
"Invoice Already Exists."), vbInformation, "Sheet Creation Error"
Exit Sub
End If
End Sub"
Any help would be much appreciated.
Regards,
Mike