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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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