VBA code to split worksheet with multiple sheets

shell2133

New Member
Joined
Jan 21, 2022
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone:). I would like to split a worksheet which has two sheets called "Position and "Assignment" at each change in column E, "Org L5", on both tabs, so create a new sheet with a position and assignment tab for each of the different Org L5s, preserving the headers and formulas etc. The headers on the Position tab are in A1:Y3, on the Assignment tab they're in A1:U1. I want to name the new sheets as per column E, the Org L5 name. I've done similar for one tab before but not two and I'm also struggling with the header on the first tab with it being in multiple rows. Would anyone know a code for this please? Thanks!
 

Attachments

  • Assignment tab.PNG
    Assignment tab.PNG
    145 KB · Views: 19
  • position tab.PNG
    position tab.PNG
    89.1 KB · Views: 19
When you add a workbook, the default number of sheets is 3. If you are only getting one sheet, you may have to change the settings in Excel. You can do that by clicking FILE...OPTIONS...GENERAL and change the number in the "Include this many sheets" box.
This version of the macro should work:
VBA Code:
Sub CreateWorkbooks()
    Application.ScreenUpdating = False
    Dim posWS As Worksheet, assWS As Worksheet, v As Variant, dic As Object, i As Long, WB As Workbook
    Set WB = ThisWorkbook
    Set posWS = WB.Sheets("Position")
    Set assWS = WB.Sheets("Assignment")
    Set dic = CreateObject("Scripting.Dictionary")
    With posWS
        v = .Range("E4", .Range("E" & .Rows.Count).End(xlUp)).Value
        For i = LBound(v) To UBound(v)
            If Not dic.exists(v(i, 1)) Then
                dic.Add v(i, 1), Nothing
                .Range("A1").CurrentRegion.AutoFilter 5, v(i, 1)
                .AutoFilter.Range.Offset(1).Copy
                Workbooks.Add
                Range("A4").PasteSpecial
                ActiveSheet.Name = "Position"
                .Range("A1").AutoFilter
                .Range("A1:Y3").Copy Range("A1")
                Columns.AutoFit
                Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Assignment"
                With assWS
                    .Range("A1").CurrentRegion.AutoFilter 5, v(i, 1)
                    .AutoFilter.Range.Copy
                    Range("A1").PasteSpecial
                End With
                With ActiveWorkbook
                    Application.DisplayAlerts = False
                    .SaveAs Filename:="C:\Users\shell\Documents\" & v(i, 1), FileFormat:=51
                    .Close False
                    Application.DisplayAlerts = True
                End With
            End If
        Next i
    End With
    assWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
That's very nearly perfect! The formula in column P on the new Position tab is still referring to the original Staff List sheet - e.g. in cell P4 its "=COUNTIFS('[STAFF LIST.xlsx]Assignment'!$H$2:$H$25,$H4)". Is there a way to change that so it just looks at the Assignment tab of the newly created sheet? So reads just "=COUNTIFS('Assignment'!$H$2:$H$25,$H4)". Is it also possible to automatically hide the new sheets Sheet2 and Sheet3 as they're just blank? Thank you for your patience with this:)
 
Upvote 0
Try:
VBA Code:
Sub CreateWorkbooks()
    Application.ScreenUpdating = False
    Dim posWS As Worksheet, assWS As Worksheet, v As Variant, dic As Object, i As Long, WB As Workbook
    Set WB = ThisWorkbook
    Set posWS = WB.Sheets("Position")
    Set assWS = WB.Sheets("Assignment")
    Set dic = CreateObject("Scripting.Dictionary")
    With posWS
        v = .Range("E4", .Range("E" & .Rows.Count).End(xlUp)).Value
        For i = LBound(v) To UBound(v)
            If Not dic.exists(v(i, 1)) Then
                dic.Add v(i, 1), Nothing
                .Range("A1").CurrentRegion.AutoFilter 5, v(i, 1)
                .AutoFilter.Range.Offset(1).Copy
                Workbooks.Add
                Range("A4").PasteSpecial
                ActiveSheet.Name = "Position"
                .Range("A1").AutoFilter
                .Range("A1:Y3").Copy Range("A1")
                Columns.AutoFit
                Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Assignment"
                With assWS
                    .Range("A1").CurrentRegion.AutoFilter 5, v(i, 1)
                    .AutoFilter.Range.Copy
                    Range("A1").PasteSpecial
                    Sheets("Position").Range("P4", Sheets("Position").Range("P" & Rows.Count).End(xlUp)).Formula = "=COUNTIFS(Assignment!$H$2:$H$" & assWS.Range("P" & Rows.Count).End(xlUp).Row & ",$H4)"
                    .Range("A1").AutoFilter
                    Columns.AutoFit
                End With
                With ActiveWorkbook
                    Application.DisplayAlerts = False
                    .Sheets("Sheet2").Delete
                    .Sheets("Sheet3").Delete
                    .SaveAs Filename:="C:\Users\shell\Documents\" & v(i, 1), FileFormat:=51
                    .Close False
                    Application.DisplayAlerts = True
                End With
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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