我试图获取一个字符串来处理代码,我自动将多个标签按照相同的标准进行过滤,以获得从其他图纸中获取小计的图纸。
所以,我做了一些If
和Else
来检查某些条件并尝试在此分析后进行过滤,而不是必须使用多个{{1}进行多个AutoFilter
}第
所以,下面的部分工作正常,它正在制作字符串(If
)
subStr
但是这部分,我避免做多个If subBacia = "TODOS" Then
subStr = """<>"""
Else
subStr = """=" & subBacia & QUOTE & " , Operator:=xlOr, Criteria2:=" & QUOTE & "=TOTAIS" & QUOTE & ""
End If
,但是没有用:
if
我的想法是,它最终会像建筑物一样,例如,有多个标准:
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=4, Criteria1:=subStr
那么,我做错了什么,或者我应该做什么,我不知道? 编辑:这里是完整的代码
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=4, Criteria1:="=AN6b" _
, Operator:=xlOr, Criteria2:="=TOTAIS"`
编辑:BTW Sub Filter()
Dim subBacia, encarregado As String
Dim bm As String
Dim subStr, encStr, bmStr As String
'Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
Sheets("Resumo").Select
encarregado = Range("T3")
subBacia = Range("T5")
bm = Range("T4")
Const QUOTE = """"
On Error Resume Next
If encarregado = "TODOS" And bm = "TODOS" And subBacia = "TODOS" Then
Sheets("Dem. Rede").Select
ActiveSheet.ShowAllData
Sheets("Dem. Interceptor").Select
ActiveSheet.ShowAllData
Sheets("Dem.Ramal").Select
ActiveSheet.ShowAllData
Else
If encarregado = "TODOS" Then
encStr = """<>"""
Else
encStr = """=" & encarregado & QUOTE & " , Operator:=xlOr, Criteria2:=" & QUOTE & "=TOTAIS" & QUOTE & ""
End If
If bm = "TODOS" Then
bmStr = """<>"""
Else
bmStr = """=" & bm & QUOTE & " , Operator:=xlOr, Criteria2:=" & QUOTE & "=TOTAIS" & QUOTE & ""
End If
If subBacia = "TODOS" Then
subStr = """<>"""
Else
subStr = """=" & subBacia & QUOTE & " , Operator:=xlOr, Criteria2:=" & QUOTE & "=TOTAIS" & QUOTE & ""
End If
Debug.Print encStr
Debug.Print bmStr
Debug.Print subStr
Sheets("Dem. Rede").Select
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=2, Criteria1:=bmStr
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=3, Criteria1:=encStr
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=4, Criteria1:=subStr
Sheets("Dem. Interceptor").Select
ActiveSheet.Range("$A$12:$EM$137").AutoFilter Field:=2, Criteria1:=bmStr
ActiveSheet.Range("$A$12:$EM$137").AutoFilter Field:=4, Criteria1:=encStr
ActiveSheet.Range("$A$12:$EM$137").AutoFilter Field:=3, Criteria1:=subStr
Sheets("Dem.Ramal").Select
ActiveSheet.Range("$B$11:$Z$1214").AutoFilter Field:=3, Criteria1:=bmStr
ActiveSheet.Range("$B$11:$Z$1214").AutoFilter Field:=2, Criteria1:=encStr
ActiveSheet.Range("$B$11:$Z$1214").AutoFilter Field:=1, Criteria1:=subStr
Sheets("Cadastro Ramal").Select
ActiveSheet.Range("$A$9:$K$841").AutoFilter Field:=2, Criteria1:=bmStr
End If
'Application.Calculation = xlCalculationAutomatic
'Application.ScreenUpdating = True
End Sub
正在返回,例如:Debug.Print
或者只是"=9" , Operator:=xlOr, Criteria2:="=TOTAIS"
它应该是它应该的方式,但是,似乎我不能按照我希望的方式连接代码中的字符串。
答案 0 :(得分:1)
您无法动态构建VBA代码片段并使用它们来补充您已硬编码的不完整语句。我认为这无处不在。您可以dynamically create a complete sub
并执行它,但我不建议这样做。
但是,无论如何您都不需要动态代码生成。您想要预先确定参数的值,因此请明确地执行此操作。诀窍是你需要明确地使用Missing
特殊值传递给你不想要的参数:
Private Type CriteriaTuple
Criteria1 As Variant
Operator As Variant
Criteria2 As Variant
End Type
Private Function MissingValue(Optional ByVal DoNotPassAnything As Variant) As Variant
If Not IsMissing(DoNotPassAnything) Then Err.Raise 5, , "I said do not pass anything"
MissingValue = DoNotPassAnything
End Function
Sub Filter()
....
dim subStr as CriteriaTuple
if ... then
subStr.Criteria1 = """<>"""
subStr.Operator = MissingValue
subStr.Criteria2 = MissingValue
else
subStr.Criteria1 = """<>"""
subStr.Operator = xlOr
subStr.Criteria2 = """=TOTAIS"""
end if
...
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=4, Criteria1:=subStr.Criteria1, Operator:=subStr.Operator, Criteria2:=subStr.Criteria2
End Sub
答案 1 :(得分:0)
除非你能另外表明,否则我认为你不能让VBA将你的字符串解释为参数列表。我还没有理解你用这种方法试图挽救的东西。