How to split a spreadsheet into multiple new spreadsheets each containing a subset of the original data?

前端 未结 2 573
故里飘歌
故里飘歌 2021-01-17 09:16

My excel spreadsheet contains

Name   Grade   Status
Paul   3       M
Paul   3       P
Paul   4       P
Steve  5       O
Steve  5       O
Nick   6       O
 .         


        
相关标签:
2条回答
  • 2021-01-17 10:00

    Try this code. I've commented it in details. But if you have some quesions, ask in comments:). Code saves new wokrbooks in the folder where your current workbook is saved.

    Sub test()
        Dim names As New Collection
        Dim ws As Worksheet, ws1 As Worksheet
        Dim wb As Workbook
        Dim lastrow As Long
        Dim cell As Range
        Dim nm As Variant
        Dim res As Range
        Dim rngHeader As Range
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        With ws
            'change "A" to column with "Names"
            lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
            'change "A" to column with "Names"
            For Each cell In .Range("A2:A" & lastrow)
                On Error Resume Next
                'collect unique names
                names.Add CStr(cell.Value), CStr(cell.Value)
                On Error GoTo 0
            Next cell
    
            'disable all filters
            .AutoFilterMode = False
    
            'change "A1:C1" to headers address of your table
            Set rngHeader = .Range("A1:C1")
    
            For Each nm In names
                With rngHeader
                    'Apply filter to "Name" column
                    .AutoFilter Field:=1, Criteria1:=nm
                    On Error Resume Next
                    'get all visible rows 
                    Set res = .Offset(2).Resize(lastrow - 1).SpecialCells(xlCellTypeVisible)
                    On Error GoTo 0
    
                    'if there is visible rows, create new WB
                    If Not res Is Nothing Then
                        'create new workbook
                        Set wb = Workbooks.Add
                        'add sheet with name form column "Names" ("Paul", "Nick" or etc)
                        wb.Worksheets.Add.name = nm
                        'delete other sheets from new wb
                        For Each ws1 In wb.Worksheets
                            If ws1.name <> nm Then ws1.Delete
                        Next
    
                        'copy/paste data
                        With wb.Worksheets(nm)
                            'copy headers
                            .Range("A1").Resize(, rngHeader.Columns.Count).Value = rngHeader.Value
                            'copy data
                            .Range("A2").Resize(res.Rows.Count, res.Columns.Count).Value = res.Value
                        End With
    
                        'save wb
                        wb.Close saveChanges:=True, Filename:=ThisWorkbook.Path & "\Spreadsheet_" & nm & ".xlsx"
                        Set wb = Nothing
                    End If
                End With
            Next
            'disable all filters
            .AutoFilterMode = False
        End With
    
        Set names = Nothing
    
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    End Sub
    
    0 讨论(0)
  • 2021-01-17 10:07

    Assuming that you have names in cells A1:A4 in Data worksheet, the formula for Paul worksheet will be:

    =IFERROR(OFFSET(INDEX(Data!$A$1:$A$4,SMALL(IF(Data!$A$1:$A$4="Paul",ROW(Data!$A$1:$A$4),""),ROW(1:1))),0,COLUMN(A:A)-1),"")
    

    Mind you, this is an array formula, which means that you have to enter it with the combination: Ctrl+Shift+Enter.

    Now you have to just fill down and to the right to as many cells as you want.

    0 讨论(0)
提交回复
热议问题