创建新工作簿并将筛选的数据复制到创建的工作簿

时间:2015-03-26 14:43:23

标签: excel-vba vba excel

我有一个数据表,其中包含各种项目经理,他们有近100名项目经理。我想编写一个VBA代码来过滤每个项目经理的数据,并创建一个新的工作簿,并将项目经理的相关数据复制到创建的项目中。工作簿。请注意项目经理的姓名可以重复。

Sub addworksheet()

Dim wsworksheet As Worksheet
Dim nwsworksheet As Worksheet
Dim wsGMworksheet As Worksheet
Dim LastRow As Long
Dim NextRow As Long
Dim i As Integer

Set wsworksheet = ThisWorkbook.Sheets("DM Names")

NextRow = wsworksheet.Range("A" & Rows.Count).End(xlUp).Row

Set wsGMworksheet = ThisWorkbook.Sheets("GMA Data")
LastRow = wsGMworksheet.Range("A" & Rows.Count).End(xlUp).Row

With wsGMworksheet

.Range("A:AN" & LastRow).AutoFilter Field:=14, Criteria1:="Abani Sahoo"
.Range("A2:AN" & LastRow).SpecialCells(xlCellTypeVisible).Rows.Copy
Sheets.Add After:=ActiveSheet
Set nwsworksheet = ActiveSheet
nwsworksheet.Range("A" & LastRow).PasteSpecial xlPasteValues
nwsworksheet.Name = wsworksheet.Range("A" & i).Value

End With

End Sub

1 个答案:

答案 0 :(得分:0)

在行中 .Range("A:AN" & LastRow).AutoFilter Field:=14, Criteria1:="Abani Sahoo"
在第一个" A"之后添加行号,即 .Range("A1:AN" & LastRow).AutoFilter Field:=14, Criteria1:="Abani Sahoo"
完全松散行
.Range("A:AN").AutoFilter Field:=14, Criteria1:="Abani Sahoo"