View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Renaming newly added worksheets

Try something like this...

Sheets("Customer Number").Copy Befo=Sheets(3)
ActiveSheet.Name = SheetName("New Customer")

'**** Add this function
Public Function SheetName(ByVal strName As String) As String
Dim lng As Long
Dim wks As Worksheet

On Error Resume Next
Set wks = Sheets(strName)
Do While Not wks Is Nothing
lng = lng + 1
Set wks = Nothing
Set wks = Sheets(strName & lng)
Loop
SheetName = strName & lng
End Function
--
HTH...

Jim Thomlinson


"Steve" wrote:

Hello. I have a button that copies an existing worksheet and names it
"New Csutomer". The problem is when the user pushes the button twice
the code errors out because there is already a sheet named "New
Customer". Is there a way to append a number to the new sheets so
this does not happen? Thanks!!

Private Sub CommandButton2_Click() 'Add new Customer Calc Sheets

Sheets("Customer Number").Copy Befo=Sheets(3)
ActiveSheet.Name = "New Customer"

End Sub