我正在尝试使用筛选选项将值从一个Excel工作表复制到另一个Excel工作表中。例如,我只使用了10条记录,但在实时情况下,我不确定将出现的数据。另外,我需要知道筛选器之后的第一个单元格值。例如,如果我使用filter,第一个值反映为B4,下一次它显示为B6。我需要选择这也是动态使用宏。
ActiveSheet.Range("$A$1:$BG$10").AutoFilter Field:=2, Criteria1:="2"
Range("B5:BG5").Select
上面的代码需要修改。它应该是行数而不是$BG$10,那么它必须是过滤器后的第一个单元格,而不是B5:BG5。
发布于 2014-01-20 16:23:05
尝试以下代码:
Sub test()
Dim lastRow As Long, firstVisibleRow As Long
ActiveSheet.AutoFilterMode = False
'find last non empty row number in column A'
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
'apply filter'
Range("$A$1:$BG$" & lastRow).AutoFilter Field:=2, Criteria1:="2"
On Error GoTo errHandler
'find first visible row number in the filtered range, if there is no rows matching the filter criteria, we'll get message from the MsgBox'
firstVisibleRow = Range("$A$2:$BG$" & lastRow).SpecialCells(xlCellTypeVisible).Row
On Error GoTo 0
'select range'
Range("B" & firstVisibleRow & ":BG" & firstVisibleRow).Select
Exit Sub
errHandler:
MsgBox "There is no rows matching the filter criteria"
End Sub
https://stackoverflow.com/questions/21228254
复制相似问题