Question:
I'm developing a macro to perform filters using VBA, however in some columns I have to deselect some values, for example:
- Column
- Paul
- Fernanda
- carla
- las
- Renata
I want everything that is different from Paulo, Fernanda and Renata. How can I do this?
I created a macro that works with two values, if I put only one, an error occurs, below is the code:
c = 1
Do While c <= coluna
If MyRange = Cells(1, c) Then
filtrocomp = InputBox("Qual o operador de comparação?" & vbCrLf & "Ex: <,<=,=,>,>=,<>", "Comparação_MAF")
filtro = InputBox("Qual o filtro de " & MyRange & " Deseja aplicar?", "Comparação_MAF")
If filtrocomp <> "" Or filtro <> "" Then
filtrosArray() = Split(filtro, ",")
Val (filtrosArray(0))
Cells(1, c).Select
Selection.AutoFilter Field:=c, Criteria1:=filtrocomp & filtrosArray(0), Operator:=xlAnd, _
Criteria2:=filtrocomp & filtrosArray(1)
Exit Do
Else
MsgBox "Nenhum filtro foi realizado!!", vbInformation, "Comparação_MAF"
End If
End If
c = c + 1
Loop
Answer:
I modified the code, it should work for a filter.
I also removed the Val (filtrosArray(0))
, it didn't do anything in your code.
c = 1
Do While c <= coluna
If MyRange = Cells(1, c) Then
filtrocomp = InputBox("Qual o operador de comparação?" & vbCrLf & "Ex: <,<=,=,>,>=,<>", "Comparação_MAF")
filtro = InputBox("Qual o filtro de " & MyRange & " Deseja aplicar?", "Comparação_MAF")
If filtrocomp <> "" Or filtro <> "" Then
filtrosArray() = Split(filtro, ",")
Cells(1, c).Select
if ubound(filtrosArray)=1 then
Selection.AutoFilter Field:=c, Criteria1:=filtrocomp & filtrosArray(0), Operator:=xlAnd, _
Criteria2:=filtrocomp & filtrosArray(1)
else
Selection.AutoFilter Field:=c, Criteria1:=filtrocomp & filtrosArray(0)
End if
Exit Do
Else
MsgBox "Nenhum filtro foi realizado!!", vbInformation, "Comparação_MAF"
End If
End If
c = c + 1
Loop