我有一个Excel表格(24列,5000行)。在VBA中,我根据critera过滤此表以查找行(目前我正在尝试使用硬编码的cretiria值来测试代码):
With Worksheets("test")
.AutoFilterMode = False
With .Range("A1:X1")
.AutoFilter
.AutoFilter Field:=2, Criteria1:="2024"
.AutoFilter Field:=3, Criteria1:="T351"
.AutoFilter Field:=4, Criteria1:="ABS5032A"
.AutoFilter Field:=11, Criteria1:="7"
.AutoFilter Field:=12, Criteria1:="8"
.AutoFilter Field:=14, Criteria1:="1002"
.AutoFilter Field:=15, Criteria1:="YES"
.AutoFilter Field:=17, Criteria1:="NO"
.AutoFilter Field:=18, Criteria1:="12"
End With
End With
这很好用,在我的Excel表格中,我可以看到右边的行可见而其他行是隐藏的。
问题从这里开始。我希望能够获得可见行的最后一列(“X”列)的值。要做到这一点,我尝试了这个(在我的过滤器之后):
Dim rsltRng As Range
Set rsltRng = Worksheets("test").AutoFilter.Range.SpecialCells(xlCellTypeVisible)
当我在另一张纸上复制rslRng时,它工作正常,右侧行被复制在工作表中:
rsltRng.Copy Worksheets("sheet").Range("A1")
但是我想避免使用另一张表,并直接获取我想要的列的值。
我知道如何做到这一点吗?
感谢您的帮助!
内拉
答案 0 :(得分:1)
那么你想要最后一行可见行X的值吗?试试这个:
Dim rsltRng As Range
Dim result As String
Dim rowNum As Integer
Dim colNum As Integer
With Worksheets("test")
.AutoFilterMode = False
With .Range("A1:X1")
.AutoFilter
.AutoFilter Field:=2, Criteria1:="2024"
.AutoFilter Field:=3, Criteria1:="T351"
.AutoFilter Field:=4, Criteria1:="ABS5032A"
.AutoFilter Field:=11, Criteria1:="7"
.AutoFilter Field:=12, Criteria1:="8"
.AutoFilter Field:=14, Criteria1:="1002"
.AutoFilter Field:=15, Criteria1:="YES"
.AutoFilter Field:=17, Criteria1:="NO"
.AutoFilter Field:=18, Criteria1:="12"
End With
Set rsltRng = .AutoFilter.Range.SpecialCells(xlCellTypeVisible)
rowNum = .Range("B1").End(xlDown).Row
colNum = 24 'column X
result = .Cells(rowNum, colNum).Text
End With