我在一个名为“Master”的Excel电子表格中有大约1,000行的数据我试图根据一个名为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相对应的行(删除其余的)?
答案 0 :(得分:1)
过滤选项卡名称上的主数据并将其复制到新工作表中。
这样的东西会起作用。您可能需要根据数据更改过滤字段。
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