VBA to dynamically add new sheets before x number of sheets

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I have the following code where it will add an upcoming years set of sheets after x number of sheets:

VBA Code:
Sub AddUOSheets()
    Dim xRg As Excel.Range
    Dim wSh As Excel.Worksheet
    Dim wBk As Excel.Workbook
    Set wSh = ActiveSheet
    Set wBk = ActiveWorkbook
   
    Application.ScreenUpdating = False
    For Each xRg In wSh.Range("A1:A5")
        With wBk
            .Sheets.Add Before:=Sheets(Sheets.Count - 144)
            On Error Resume Next
            ActiveSheet.Name = xRg.Value
            If Err.Number = 1004 Then
              Debug.Print xRg.Value & " already used as a sheet name"
            End If
            On Error GoTo 0
        End With
    Next xRg
    Application.ScreenUpdating = True
   
    Sheets("UO Sheets Adder").Activate
End Sub

Right now the code has a static number 144 in the line:

.Sheets.Add Before:=Sheets(Sheets.Count - 144)

Is there a way to make this dynamic so that it will add 5 to the previous number after the macro is used? After the next time the macro is used the line should then read:

.Sheets.Add Before:=Sheets(Sheets.Count - 149)

Thanks in advance for your assistance!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Perhaps have a hidden sheet that simply contains a starting # (e.g. 144) in cell A1, then reference and add to it every time the macro is run. Perhaps something like:
VBA Code:
.Sheets.Add Before:=Sheets(Sheets.Count - Sheets("YourHiddenSheetName").Range("A1").Value)
VBA Code:
    Next xRg
    Sheets("YourHiddenSheetName").Range("A1").Value = Sheets("YourHiddenSheetName").Range("A1").Value + 5
    Application.ScreenUpdating = True
 
Upvote 0
Solution
Thank you sooo much Z51! That did the trick!
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,805
Members
449,127
Latest member
Cyko

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