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.
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