Loop through the filters and create a new workbook for each filter

question610

New Member
Joined
Jul 3, 2017
Messages
29
I have a workbook with Raw data about many firms.The two identifying factors of the firms are it's name and it's ID number.

The manual process would be to create a filter on the firm name or firm id number and then manually go through each firm one by one. Copy and Paste each firm's information into a new workbook and save that workbook. So in the end,I would end up with 20 new workbooks where each workbook is about one firm.

What is the best way to go about this using vba? How does one loop through the filters? Should I filter and then copy and paste that info into a new sheet and then loops through the sheets to create a new file?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hia
As you haven't given any info about sheet name or ranges
This runs on the active sheet & looks in Column A for the Company name
Code:
Sub CoSplit()

    Dim CoRng As Range
    Dim CoDict As Object
    Dim Co As Range
    Dim CoLst As Variant
    Dim UsdRws As Long
    Dim MainSht As Worksheet
    Dim i As Long
    
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    
    Set MainSht = ActiveSheet
    Set CoDict = CreateObject("Scripting.Dictionary")
    
    With CoDict
        For Each Co In MainSht.Range("A2:A" & UsdRws)
            If Not .exists(Co.Text) Then .Add Co.Text, Nothing
        Next Co
        CoLst = .keys
    End With
    
    With MainSht.Range("A1")
        .AutoFilter
        For i = 0 To CoDict.Count - 1
            .AutoFilter Field:=1, Criteria1:=CoLst(i)
            Workbooks.Add 1
            .CurrentRegion.Copy Range("A1")
            ActiveWorkbook.SaveAs Filename:=CoLst(i)
            ActiveWorkbook.Close
        Next i
    End With
    

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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