如何将数据粘贴到新的工作表中,并根据另一个表中的单元格删除某些行?

我在一个名为“主”的Excel电子表格中有大约1000行的数据,我试图根据称为区域ID的标识符将其分成42个不同的电子表格。 我在同一个工作簿的另一个电子表格中创build了一个包含42个独特不同区域的表格,以及我想命名新工作表的内容。 到目前为止,我有以下代码,它创build了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 

如何在新创build的工作表中粘贴“主”电子表格内容,并只保留对应于区域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