Help with macro

John T

Board Regular
Joined
Nov 28, 2013
Messages
145
Office Version
  1. 365
Platform
  1. Windows
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

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
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
just a thought

ActiveSheet.Name = wsName
ActiveSheet.Unprotect Password:="1234"
wsName.Range("s13").Value = i
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,973
Members
448,933
Latest member
Bluedbw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top