Vba 如何将数据粘贴到新工作表中,并基于其他表中的单元格删除某些行?

Vba 如何将数据粘贴到新工作表中,并基于其他表中的单元格删除某些行?,vba,excel,Vba,Excel,我在一个名为“Master”的Excel电子表格中有大约1000行数据,我正试图根据一个名为Region ID的标识符将其拆分为42个不同的电子表格。我在同一工作簿的另一个电子表格中创建了一个表,其中包含42个唯一的不同区域,以及我想要命名的新工作表。到目前为止,我有以下代码,它创建了42个新的电子表格并相应地命名它们 Sub Create_Tabs() Dim MyCell As Range, MyRange As Range Set MyRange = Sheets("TabList").

我在一个名为“Master”的Excel电子表格中有大约1000行数据,我正试图根据一个名为Region ID的标识符将其拆分为42个不同的电子表格。我在同一工作簿的另一个电子表格中创建了一个表,其中包含42个唯一的不同区域,以及我想要命名的新工作表。到目前为止,我有以下代码,它创建了42个新的电子表格并相应地命名它们

Sub Create_Tabs()
Dim MyCell As Range, MyRange As Range

Set MyRange = Sheets("TabList").Range("D2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))

For Each MyCell In MyRange
    Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
    Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
    'I would like to create code here to paste the "Master" spreadsheet contents in the newly created worksheet and keep only rows corresponding Region IDs (delete the rest).
Next MyCell
End Sub

如何将“主”电子表格内容粘贴到新创建的工作表中,并仅保留对应区域ID的行(删除其余行)?

根据选项卡名称筛选主数据,并将其复制到新工作表中

像这样的东西会有用的。您可能需要根据数据更改筛选字段

Sub Create_Tabs_And_Copy_Data()
    Dim MyCell As Range, MyRange As Range

    Set MyRange = Sheets("TabList").Range("D2")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
        'I would like to create code here to paste the "Master" spreadsheet contents in the newly created worksheet and keep only rows corresponding Region IDs (delete the rest).
         With Sheets("Master").UsedRange
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:=MyCell.Value
            .SpecialCells(xlCellTypeVisible).Copy Sheets(MyCell.Value).Cells(1, 1)
            Application.CutCopyMode = False
        End With
    Next MyCell
    End Sub