Add multiple sheets before multiple specific 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
So I found this code to add multiple sheets however would like to modify it to add a second range (B1:B30) where it would now add the sheets before each specific sheet in the new second range. Can this be done?

VBA Code:
Sub AddSheets()
    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:A30")
        With wBk
            .Sheets.Add after:=.Sheets(.Sheets.Count)
            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("Sheet Add").Activate
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
So if you have Sheet_A, Sheet_B, Sheet_C, Sheet_D etc in Column B, you want to add a sheet and name it Sheet_A and place it where?
Sheet_B needs to be placed before Sheet_A? Sheet_C needs to be before Sheet_B? etc etc
What do you want to happen if a sheet with one of these names exist already?
 
Upvote 0
So right now I have sheets 2022 Location A Actual, 2021 Location A Actual, 2020 Location A Actual, 2022 Location A Budget, 2021 Location A Budget, 2020 Location A Budget, 2022 Location A PO, 2021 Location A PO, 2020 Location A PO in order. This is also repeated for Locations B -J and as you can imagine, there are a ton of sheets already in the workbook. The purpose of this code is to add 2023 sheets but to keep them grouped together with their respective Locations and Actual/Budget/PO. Ideally the code would take the names in range A1:A30 (ie 2023 Location A Actual, 2023 Location A Budget, 2023 Location A PO) and place them before their respective 2022 sheets (ie 2022 Location A Actual, 2022 Location A Budget, 2022 Location A PO) which are listed in range B1:B30.

Hope this makes sense
 
Upvote 0

Forum statistics

Threads
1,214,807
Messages
6,121,679
Members
449,047
Latest member
notmrdurden

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