简体   繁体   English

VBA 根据每张表中的 A 列过滤器复制多个工作表并创建新工作簿

[英]VBA Copy multiple sheets based on column A filter in each sheet and create new workbook

I have 3 consolidated sheet in a workbook which I need to segregate into 3 sheets in new workbook based on unique values in column A of each sheet:我在工作簿中有 3 个合并的工作表,我需要根据每张工作表的 A 列中的唯一值将其分成新工作簿中的 3 个工作表:

In the "A" workbook, all 3 sheets each sheet should have only its information and needs to loop for all names.在“A”工作簿中,每张纸的所有 3 张纸都应该只有其信息,并且需要为所有名称循环。

Below the code that only moves data from workbook to workbook, but is not much helpful.下面的代码仅将数据从工作簿移动到工作簿,但没有太大帮助。

例子

Backup Worksheets by Name按名称备份工作表

  • This is a somewhat simplified example that assumes that each table starts in A1 , that the worksheets are not filtered, that the names are in column 1 ("A"), that the first worksheet ( Sales ) contains all the unique values (names),...这是一个稍微简化的示例,假设每个表都以A1开头,工作表未过滤,名称位于第 1 列(“A”)中,第一个工作表( Sales )包含所有唯一值(名称) ,...

  • For each unique value (name) it copies only the worksheets from the list to a new workbook.对于每个唯一值(名称),它仅将列表中的工作表复制到新工作簿中。 Then it loops through all the worksheets in the new workbook and deletes the rows that do not contain the value leaving the headers intact.然后它遍历新工作簿中的所有工作表并删除不包含值的行,保留标题不变。 Finally, it saves the new workbook.最后,它保存新的工作簿。

Option Explicit

Sub BackupByName()
    
    Const wsNamesList As String = "Sales,Marketing,Operations"
    Const First As String = "A1" ' You cannot change this...1
    
    Dim wsNames() As String: wsNames = Split(wsNamesList, ",")
    
    Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
    Dim dFolderPath As String: dFolderPath = swb.Path & "\"
    
    ' Assuming that "Sales" contains all names.
    Dim ws As Worksheet: Set ws = swb.Worksheets(wsNames(0))
    
    Dim rg As Range: Set rg = RefColumn(ws.Range(First).Offset(1))
    If rg Is Nothing Then Exit Sub ' range reference cannot be created
    
    Dim Data As Variant: Data = GetRange(rg)
    
    Dim uData As Variant: uData = ArrUniqueData(Data)
    If IsEmpty(uData) Then Exit Sub ' no unique values
    
    Dim uUpper As Long: uUpper = UBound(uData)
    
    Application.ScreenUpdating = False
    
    Dim dwb As Workbook
    Dim dws As Worksheet
    Dim drg As Range
    Dim n As Long
    Dim nName As String
    Dim dName As String
    
    For n = 0 To uUpper
        swb.Worksheets(wsNames).Copy
        Set dwb = ActiveWorkbook
        nName = uData(n)
        For Each dws In dwb.Worksheets
            '1... because of these simplifications.
            Set rg = dws.Range(First).CurrentRegion.Columns(1)
            rg.Columns.AutoFilter 1, "<>" & Name
            Set drg = Nothing
            On Error Resume Next
            Set drg = rg.Resize(rg.Rows.Count - 1).Offset(1) _
                .SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not drg Is Nothing Then
                drg.EntireRow.Delete
            End If
            dws.AutoFilterMode = False
        Next dws
        dName = dFolderPath & nName & ".xlsx"
        Application.DisplayAlerts = False
        dwb.SaveAs Filename:=dName, FileFormat:=xlOpenXMLWorkbook ' 51
        Application.DisplayAlerts = True
        dwb.Close SaveChanges:=False
    Next n

    Application.ScreenUpdating = True

End Sub
   
Function RefColumn( _
    ByVal FirstCellRange As Range) _
As Range
    If FirstCellRange Is Nothing Then Exit Function
    With FirstCellRange.Cells(1)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If lCell Is Nothing Then Exit Function
        Set RefColumn = .Resize(lCell.Row - .Row + 1)
    End With
End Function
   
Function GetRange( _
    ByVal rg As Range) _
As Variant
    If rg Is Nothing Then Exit Function
    
    Dim rData As Variant
    If rg.Rows.Count + rg.Columns.Count = 2 Then
        ReDim rData(1 To 1, 1 To 1): rData(1, 1) = rg.Value
    Else
        rData = rg.Value
    End If

    GetRange = rData
End Function

Function ArrUniqueData( _
    Data As Variant, _
    Optional ByVal CompareMethod As VbCompareMethod = vbTextCompare) _
As Variant
    
    If IsEmpty(Data) Then Exit Function
    
    Dim cLower As Long: cLower = LBound(Data, 2)
    Dim cUpper As Long: cUpper = UBound(Data, 2)
    
    Dim Key As Variant
    Dim r As Long
    Dim c As Long
    With CreateObject("Scripting.Dictionary")
        .CompareMode = CompareMethod
        For r = LBound(Data, 1) To UBound(Data, 1)
            For c = cLower To cUpper
                Key = Data(r, c)
                If Not IsError(Key) Then
                    If Len(Key) > 0 Then
                        .Item(Key) = Empty
                    End If
                End If
            Next c
        Next r
        If .Count = 0 Then Exit Function
        ArrUniqueData = .Keys
    End With

End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA - 根据汇总 Excel 表格上的条件,将工作簿中的不同模板工作表复制到另一个工作簿的多张工作表中 - VBA - copy different template sheets from a workbook, into multiple sheets of another workbook based on criteria on a summary excel sheet 为多个工作表创建VBA,并使用公式将粘贴复制到新列中 - Create VBA for multiple sheet and copy paste in new column with formula 根据自动筛选条件将工作簿中的多个工作表复制到汇总表 - Copy multiple sheets in a workbook to a summary sheet based on autofilter criteria 将所有工作簿工作表复制到新工作簿 VBA - copy all workbook sheets to a new workbook VBA VBA 将工作表复制到多个工作簿 - VBA copy sheet to multiple workbook 用于在工作簿中的多个工作表上复制和粘贴最后一列数据的 VBA 脚本 - VBA Script to copy and Paste last column of data on multiple sheets in a workbook VBA 将多张纸从一个 wb 复制到新的 wb(每张纸一个 wb) - VBA to copy multiple sheets from one wb into new wb's (one wb for each sheet) 筛选多个工作表(同一工作簿)中的数据并复制另一个工作簿的工作表中的数据 - Filter data in multiple sheets (same workbook) and copy data in sheet of another workbook VBA 用于过滤的宏,从列中复制指定值并创建然后粘贴到具有该列名称的新工作表中 - VBA Macro to filter, copy the specified value from a column and create then paste in a new sheet with that column name Excel VBA根据列表从多个工作表复制工作表数据 - Excel VBA copy sheet data from multiple sheets based on list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM