Solved

MS Excel dividing rows by a sort column

Posted on 2014-02-04
2
229 Views
Last Modified: 2014-02-25
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.

John
0
Comment
Question by:j_heck
  • 2
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39834272
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
rgUniques.ClearContents
If rgUniques.Rows.Count = 1 Then Exit Sub

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

Open in new window

0
 
LVL 81

Expert Comment

by:byundt
ID: 39834282
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.
FilterToNewWorksheetsQ28356225.xlsm
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question