我正在一个项目中,我需要根据另一张表中提到的某个值和列名来过滤数据
我的数据在名为“包含表”的表中,表名称为“包含”
我写了以下代码
'''
Dim r1 As Integer
r1 = Rows("1").Find("MatchA").Column
Sheets("Inclusion Sheet").Select
ActiveSheet.Range("Inclusion").AutoFilter Field:=r1, Criteria1:="TRUE"
'''
在此r1中,直接使用单元名称“ MatchA”。我要更改此值,以使r1的值基于单元格A1,该单元格是在数据表本身之外的另一张表(例如Sheet1)中写入的。
因此,如果我更改工作表1中的单元格A1的值,则r1的值也应相应更改
答案 0 :(得分:0)
这应该做到:
Option Explicit
Sub Test()
Dim r1 As Long 'don't use integer, integers are Longs cut to an integer
Dim MySearch As String 'here you will get the string you are looking for
With ThisWorkbook.Sheets("Sheet1")
MySearch = .Range("A1") 'If I understood your question, the column you are looking for is in Range A1 from Sheet1
End With
With ThisWorkbook.Sheets("Inclusion Sheet")
r1 = .Range("Inclusion").Find(MySearch).Column
.Range("Inclusion").AutoFilter Field:=r1, Criteria1:="TRUE"
End With
End Sub