Ok i have spent nearly 2 hours looking through posts and trying to piece together what I need. I would be extremely appreciative if someone can help me with my code which doesn't work. I want it to copy the sheet "template" and create a copy named 1, if one exists then rename 2, if 2 exists then 3 and so forth.
When I get it to work it will loop through and go from 1 to 6 but not create sheets 2-5 for some reason.
I would also like to have it add the sheet in the "3rd Position" of the worksheets in my workbook, instead of adding it to the end. If this is possible, please help as well.
When I get it to work it will loop through and go from 1 to 6 but not create sheets 2-5 for some reason.
I would also like to have it add the sheet in the "3rd Position" of the worksheets in my workbook, instead of adding it to the end. If this is possible, please help as well.
Code:
Sub CopySheet()
Dim ws As Worksheet
Set ws = Sheets("Template")
ws.Copy After:=Sheets(Sheets.Count)
newname = Null
newname = 1
Do
If SheetExists(newname) Then
Exit Do
Else
newname = newname + 1
End If
Loop
ActiveSheet.Name = newname
End Sub
Private Function SheetExists(newname) As Boolean
' Returns TRUE if sheet exists in the active workbook
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(newname)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
End Function