[VBA] [Question] Split the sheet to multiple workbooks base on 2 column

zeromax1

Board Regular
Joined
Mar 20, 2020
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Hi all, I would like to find out the VBA code that can split the excel sheet into multiple workbooks by 2 columns.

Thank you Fluff share the below VBA code that can split the sheets base on 1 column. But I have a case that need to split the file base on 2 columns. When column A meet the specific criteria that the file will split by column B.

Eg: Column A is the list of countries and column B is the list of city. I only want to split the sheet into multiple workbooks for those USA cities.

VBA Code:
Sub Split()
   Dim cl As Range
   Dim WS As Worksheet
   Dim rng As Range
    
   Application.ScreenUpdating = False
  
   Set WS = ThisWorkbook.Worksheets(1)
   Filename = ThisWorkbook.Name
   If InStr(Filename, ".") > 0 Then
        Filename = Left(Filename, InStr(Filename, ".") - 1)
    End If
    
   If WS.FilterMode Then WS.ShowAllData
   With CreateObject("scripting.dictionary")
      For Each cl In WS.Range("A9", WS.Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(cl.Value) Then
            .Add cl.Value, Nothing
            WS.Copy
            Range("A8").AutoFilter 1, "<>" & cl.Value
            Range("A9:A3000").SpecialCells(xlVisible).EntireRow.Delete
            ActiveSheet.ShowAllData
            ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Filename & " " & "(" & cl.Value & ")" & ".xlsx", 51
            ActiveWorkbook.Close False
         End If
        
                
      Next cl

   End With
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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