Excel.Tips.Net Welcome toExcel.Tips.Net

Helpful Links

Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment

Tips.Net Store

ExcelTips FAQ
ExcelTips Premium

Learn Access Now
Free Printable Forms

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

Newest Tips

Recording a Macro

Adding a Little Animation to Your Life

Converting a Range of URLs to Hyperlinks

Making the Formula Bar Persistent

Engineering Calculations

Digital Signatures for Macros

Fixing the Decimal Point

 

Putting Addresses on State-Specific Worksheets

Summary: Got a bunch of data on a worksheet that you need to divide onto other worksheets, based on a specific piece of information in the data? You can do this manually or you can do it using a macro that examines your data and makes the move. Here's how. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

Linda has a worksheet containing 250 addresses from all over the country. She wants to separate the addresses, by state, to different worksheets so that each worksheet contains addresses only from a specific state.

There is no way to do this with any of the built-in Excel functions or wizards. This is probably a reflection of the fact that most people leave the data on a single worksheet, and then use various Excel tools (such as filtering) to display only a subset of the overall data.

If you want to copy state-specific information to separate sheets, however, you can do so manually by using AutoFilter. This works particularly well if you have only a few states in your sources data. Just apply an AutoFilter and display only those rows that are in the state you want to copy. Select the visible rows, copy them, and paste them to a new worksheet. Repeat the process with each of the other states in your original data set.

If you have data for quite a few sheets, you can copy it by automating this process. The following macro will use the AutoFilter capabilities of Excel to copy the information to a new worksheet. It does this for each unique value in the state column, which is specified by the iCol variable. (In this example, iCol is set to 5, which means that the states are in column E.)

Sub NewSheetEachAutofilter()
    Dim wOri As Worksheet
    Dim wks As Worksheet
    Dim wPT As Worksheet
    Dim bAutoFilter As Boolean
    Dim PT As PivotTable
    Dim rPT As Range
    Dim rCell As Range
    Dim iCol As Integer
    Dim sHeader As String

    On Error GoTo Errhandler
    Application.ScreenUpdating = False
    iCol = 5 'Filter all on Col E

    Set wOri = ActiveSheet
    With wOri
        'Save Autofilter status
        bAutoFilter = .AutoFilterMode
        If Not bAutoFilter Then 'turn on autofilter
            .Range("a1").AutoFilter
        End If
        If .FilterMode Then .ShowAllData

        'use a PivotTable on a temp
        'sheet to get a unique list
        Set PT = .PivotTableWizard _
          (SourceType:=xlDatabase, _
          SourceData:=.Range("a1").CurrentRegion, _
          TableDestination:="", _
          TableName:="PivotTable1")
        sHeader = .Cells(1, iCol)
        With PT
            .AddFields RowFields:=sHeader
            .PivotFields(sHeader).Orientation = xlDataField
            .ColumnGrand = False
        End With
        Set wPT = ActiveSheet
        With wPT
            Set rPT = .Range(.Range("A3"), _
              .Cells(.Cells.Rows.Count, 1).End(xlUp))
        End With

        'loop through unique list
        For Each rCell In rPT
            .Range("a1").AutoFilter Field:=iCol, _
              Criteria1:=rCell.Value
            'create new sheet and name it with the state
            Set wks = Worksheets.Add
            wks.Name = rCell.Value
            .AutoFilter.Range.Copy wks.Range("A1")
        Next
        .ShowAllData
        .Select
    End With
    Application.DisplayAlerts = False
    wPT.Delete

ExitHandler:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    'remove filter if no previous one
    If Not bAutoFilter Then
        wOri.AutoFilterMode = False
    End If
    Set rCell = Nothing
    Set rPT = Nothing
    Set PT = Nothing
    Set wOri = Nothing
    Set wks = Nothing
    Set wPT = Nothing
    Exit Sub

Errhandler:
    MsgBox Err.Number & ":" & Err.Description
    Resume ExitHandler
End Sub

The code may look complex because of its length, but it isn't particularly difficult. It makes sure that the AutoFilter is turned on, and then it creates a PivotTable based on your original data. This PivotTable is used to gather the list of states from the data. Each state is then used on the original data as a filtering criteria. The filtered information is then copied to a new worksheet that is named using the state.

The macro does not modify the original data. If you prefer to have the original data deleted after it is moved to a worksheet, then all you need to do is add a single line of code. Add this line right after the line that deletes the PivotTable (wPT.Delete):

wOri.Delete

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3383) applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003 | 2007

Your Data, Your Way! Want the greatest control possible over how your data appears on the page? Excel's custom formats can provide that control, and ExcelTips: Custom Formats can unlock the secrets to creating your own custom formats.
 
Check out ExcelTips: Custom Formats today!