Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

MS Excel dividing rows by a sort column

I have an Excel spreadsheet with 15,000+ rows with 15 columns and it is sorted by BillToMfgName, MfgName, StoreNbr, Category, and ItemId.  Is there a way (macro or some other) to automate the seperation of all rows for one manufacturer by the BillToMfgName column and put that information into a new spreadsheet?  And do this for each BillToMfgName in some type of automated way?

Thanks in advance for the help.

  • 2
1 Solution
Here is a macro that uses Advanced Filter to capture a list of unique BillToMfgName, then uses AutoFilter to filter your data for each manufacturer in turn. The filtered data is copied to a new worksheet named after the manufacturer.

As written, the macro runs on the active worksheet, and assumes that your data are surrounded by empty rows and columns.
Sub BillToMfgToWorksheets()
Dim cel As Range, rg As Range, rgFilt As Range, rgUniques As Range
Dim iCol As Long
Dim v As Variant, vUniques As Variant
Dim ws As Worksheet
Application.ScreenUpdating = False
Set cel = ActiveCell
Set rg = ActiveSheet.Range("A1").CurrentRegion  'All the raw data
v = Application.Match("BillToMfgName", rg.Rows(1), 0)
If IsError(v) Then Exit Sub

iCol = v
Set rgUniques = rg.Cells(1, 1).Offset(0, rg.Columns.Count + 2)
Set rgFilt = rg.Columns(iCol)
rgFilt.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=rgUniques, Unique:=True
Set rgUniques = rgUniques.Resize(rgUniques.CurrentRegion.Rows.Count, 1)
vUniques = rgUniques.Offset(1, 0).Resize(rgUniques.Rows.Count - 1).Value
If rgUniques.Rows.Count = 1 Then Exit Sub

For Each v In vUniques
    rg.AutoFilter Field:=iCol, Criteria1:=v
    Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
    ws.Name = v
    rg.Copy ws.Range("A1")
Application.Goto cel
End Sub

Open in new window

Here is a sample workbook containing the macro. Push the button to see it in action.

The macro is installed on a regular module sheet.

If you want to test the macro with your data, just paste it over my test data.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now