Excel VBA定义过滤范围的最后一行
我正在试图find工作表上过滤范围的行数。 LstRow2是我试图find的variables。 有了这段代码,我得到了未经过滤的行数。
CSht.Range(CSht.Cells(1, 1), CSht.Cells(LstRow1, LstCol1)).AutoFilter Field:=2, Criteria1:="RA" With CSht LstRow2 = .UsedRange.SpecialCells(xlCellTypeLastCell).Row End With
您只需要使用可见单元格,因为它已被过滤。
尝试这个:
With CSht 'load filter cells into Range object Dim rngFilter as Range Set rngFilter = Intersect(.UsedRange,.UsedRange.Offset(1)).SpecialCells(xlCellTypeVisible) 'find the max number of elements split by $ in the range address 'needs to be dynamic because of areas Dim lUpper as Long lUpper = UBound(Split(rngFilter.Address,"$")) 'the last element will be the last row in the filtered range 'the last number in the rngFilter.Address Dim LstRow2 as Long LstRow2 = Split(rngFilter.Address,"$")(lUpper) End With
你为什么不更换这条线?
LstRow2 = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
同
LstRow2 = .Cells(.rows.count, 1).end(xlup).row
当前区域将在一行中为您完成
LastRow = sht.Range("A1").CurrentRegion.Rows.Count