我有一个电子表格,其中的列是动态的,我需要首先找到列标题,然后过滤数据,到目前为止,我的代码如下:
Sub DynamicFilter222()
SearchCol = "Measurement Profile"
SearchCol2 = "Count"
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = ActiveSheet.UsedRange.Find(SearchCol, , xlValues, xlWhole)
Set rng2 = ActiveSheet.UsedRange.Find(SearchCol2, , xlValues, xlWhole)
Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=rng1.Column, Criteria1:="=Shear Rated(gamma)/dt
= 4 1/s", Operator:=xlOr, Criteria2:="="
Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=rng2.Column, Criteria1:="Viscosity",
Operator:=xlOr, Criteria2:="="
End Sub
但是只有第二部分有效,如果我删除第二部分,那么第一部分将正确过滤
因此定义: 我需要在A1行中搜索列标题(“测量配置文件”和“计数”),找到后,我需要过滤“测量配置文件”列以仅包括BLANKS和“ Shear Rating(gamma)/ dt = 4 1 / s”和“计数”列中仅包含空白和“粘度”
非常感谢我尝试使用Application.Worksheetfunction.Match的任何帮助,但到目前为止我都没有用,
感谢您的帮助!
Dim rng As Range, res As Variant, count As Variant
Set rng = ActiveWorkbook.ActiveSheet
res = Application.Match("=Measurement Profile", rng, 0)
count = Application.Match("Count", rng, 0)
If Not IsError(res) Then
rng.AutoFilter Field:=res, Criteria1:="=Shear Rated(gamma)/dt = 4 1/s",
Operator:=xlOr, Criteria2:="="
End If
If Not IsError(count) Then
rng.AutoFilter Field:=count, Criteria1:="Viscosity", Operator:=xlOr,
Criteria2:="="
End If
End Sub
对此进行了尝试,并在“设置rng = ActiveWorkbook.ActiveSheet”上显示“类型不匹配错误” (第2行)
使用以下解决方案更改已解决此问题 将“设置rng = ActiveWorkbook.ActiveSheet”设置为“将rng = ActiveWorkbook.ActiveSheet.Rows(1)”
但是,我现在遇到这样的问题:仅过滤一列,“计数”列被完美过滤以仅显示“粘度”和“空白”,但是“测量配置文件”列显示的不仅仅是“剪切”额定(gamma)4 1 / s”和“空白”
答案 0 :(得分:0)
1)更改
Set rng = ActiveWorkbook.ActiveSheet
到
Set rng = ActiveWorkbook.ActiveSheet.Rows(1)
因为(正确)声明了Dim rng As Range
,而您正在为其分配工作表对象(Set rng = ActiveWorkbook.ActiveSheet
)
2)并进行更改(可能)
res = Application.Match("=Measurement Profile", rng, 0)
到
res = Application.Match("Measurement Profile", rng, 0)