I was trying to get a string to work with a code where I automate multiple tabs to be filtered by the same criteria, for a sheet that gets the subtotal out of other sheets.
So, I did some If and Elses to check for some criteria and try to make the filtering after this analysis, instead of having to do multiple AutoFilters withing multiple Ifs.
So, the part bellow works just fine, it is making the string (subStr) accordingly
If subBacia = "TODOS" Then
subStr = """<>"""
Else
subStr = """=" & subBacia & QUOTE & " , Operator:=xlOr, Criteria2:=" & QUOTE & "=TOTAIS" & QUOTE & ""
End If
But this part, that I am avoiding to do multiple ifs for, is not working:
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=4, Criteria1:=subStr
My idea was that it would end up as the construction bellow, for example, with multiple criteria:
ActiveSheet.Range("$A$12:$EX$2025").AutoFilter Field:=4, Criteria1:="=AN6b" _
, Operator:=xlOr, Criteria2:="=TOTAIS"`
So, what am I doing wrong, or what should I do that I am not aware of? Edit: Here goes the full code
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
Edit: BTW the Debug.Print is returning for example: "=9" , Operator:=xlOr, Criteria2:="=TOTAIS"
or simply "<>"
And it is exactly the way it should, but, it seems I can't concatenate the string in the code the way I hoped for.