Hi, I have the following macro that copies the master sheet and adds a sequential number every time its run.
I also need it to replicate the sheet number in cell S13.
My macro works but for some reason the sheet numbers change after i insert the second sheet.
The worksheet numbers represent the invoice number and that is what will also appear in cell S13.
Is there anything obviously wrong with my code?
Thanks
I also need it to replicate the sheet number in cell S13.
My macro works but for some reason the sheet numbers change after i insert the second sheet.
The worksheet numbers represent the invoice number and that is what will also appear in cell S13.
Is there anything obviously wrong with my code?
Thanks
Code:
Sub AddWs()
Application.ScreenUpdating = False
Sheets("Master").Visible = True
ActiveSheet.Unprotect
Dim i As Long, wsName As String, temp As String
Sheets("MASTER").Copy after:=Sheets(Sheets.Count)
wsName = "2101"
If WorksheetExists(wsName) Then
temp = Left(wsName, 3)
i = 1
wsName = temp & i
Do While WorksheetExists(wsName)
i = i + 1
wsName = temp & i
Loop
End If
ActiveSheet.Name = wsName
ActiveSheet.Unprotect Password:="1234"
ActiveSheet.Range("s13").Value = i
Sheets("Master").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("Master").Visible = False
Sheets(Sheets.Count).Select
ActiveWindow.SmallScroll Down:=-3
Range("B13").Select
Application.ScreenUpdating = True
End Sub
Function WorksheetExists(wsName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(wsName).Name = wsName
On Error GoTo 0
End Function