我正在尝试过滤选项卡"到期合同"中的数据集,在B列上过滤(此数据集可以根据任何点增加或减少)。不同的过滤器来自标签"输入"这可以改变加班(增加或减少)。我试图将过滤器的结果粘贴到单独的选项卡,其名称与列表完全相同,但我想将值粘贴到下一个可用(空白)单元格上。这就是我现在所拥有的:
Sub ParseList2()
Dim uwname As String
Dim lastrowUW As Long
Dim lastrow As Long
Dim N As Range
lastrowUW = Sheets("Inputs").Cells(Rows.Count, "H").End(xlUp).Row
For Each N In Sheets("Inputs").Range("H2:H" & lastrowUW).Cells
uwname = N.Text
Sheets("Expiring Contracts").Range("$A:$AA").AutoFilter Field:=2,
Criteria1:=N
lastrow = Columns(2).Find("*", SearchDirection:=xlPrevious).Row
Range("A2:AA" & lastrow).SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets(uwname).Select
lastrow = Columns(2).Find("*", SearchDirection:=xlPrevious).Row + 1
Range("A" & lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next N
Sheets("Expiring Contracts").AutoFilterMode = False
End Sub
这要归功于下面的一些评论!
Sub ParseList2()
Dim uwname As String
Dim lastrowUW As Long
Dim lastrow As Long
Dim N As Range
Dim rng As Range
lastrowUW = Sheets("Inputs").Cells(Rows.Count, "H").End(xlUp).Row
For Each N In Sheets("Inputs").Range("H2:H22").Cells
uwname = N.Value
Sheets("Expiring Contracts").Range("$A:$AA").AutoFilter Field:=2,
Criteria1:=uwname
'lastrow = Columns(2).Find("*", SearchDirection:=xlPrevious).Row + 1
Range("A2:AA99999").SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets(uwname).Select
lastrow = Columns(2).Find("*", SearchDirection:=xlPrevious).Row + 1
Range("A" & lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Expiring Contracts").Select
Sheets("Expiring Contracts").AutoFilterMode = False
Range("A1").Select
Next N
Sheets("Expiring Contracts").Select
Sheets("Expiring Contracts").AutoFilterMode = False
Range("A1").Select
End Sub
答案 0 :(得分:0)
使用数据透视表和切片器可以很容易地完成使用VBA尝试做的事情。将源数据转换为Excel表,从中创建数据透视表,将数据透视表放在“输入”选项卡中,在要筛选的字段上设置切片器,将数据透视表中的其他感兴趣的字段作为行字段,而且你已经完成了。没有必要的代码。让应用程序为您完成工作。