Solved

MS Excel dividing rows by a sort column

Posted on 2014-02-04
2
205 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 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now