如何将电子表格拆分为多个新电子表格,每个电子表格都包含原始数据的子集?

Don*_*llo -9 excel vba

我的Excel电子表格包含

Name   Grade   Status
Paul   3       M
Paul   3       P
Paul   4       P
Steve  5       O
Steve  5       O
Nick   6       O
 ........
Run Code Online (Sandbox Code Playgroud)

我用过freeze panel和其他格式化的东西.


我想创建单独的Spreadsheets只包含一个名称.例:

  1. Spreadsheet_paul.xls

    Name   Grade   Status
    Paul   3       M
    Paul   3       P
    Paul   4       P
    
    Run Code Online (Sandbox Code Playgroud)
  2. Spreadsheet_Nick.xls

    Name   Grade   Status
    Nick   6       o
    
    Run Code Online (Sandbox Code Playgroud)

    .........

我需要创建单独的文件,最后的文件数量等于原始电子表格中的名称数量,每个文件包含原始数据的相应子集.

我怎样才能做到这一点 ?

Dmi*_*liv 10

试试这个代码.我已经详细评论了它.但如果你有一些问题,请在评论中提问:).代码将新的wokrbooks保存在保存当前工作簿的文件夹中.

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
Run Code Online (Sandbox Code Playgroud)

  • `rngHeader.Copy Destination:=.Range("A1")`和`res.Copy Destination:=.Range("A2")` (2认同)
  • 谢谢,真棒 (2认同)