Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

fixing error type mismatch filter data in table based two values

0

hello 

i have  this  code  to filter data in table  based on two  values f1,g1  in column a  but  it  gives me error   type mismatch      i  hope  some  body fixing 

Sub VV()
Dim a As Long, b As Long
a = Sheets("LTSB").Range("F1").Value 'Change to cell of your choice
b = Sheets("LTSB").Range("G1").Value 'Change to cell of your choice
Sheets("LTSB").ListObjects("Tabel1").Range.AutoFilter Field:=1, Criteria1:= _
 a, Operator:=xlAnd, Criteria2:=b
End Sub

thanks in advance 

Answer
Discuss

Answers

0
Selected Answer

Leopard

Two small problems here... 1) you set your variables as Long (numbers) but F1 and G1 contain strings; 2) The default operator for filters is xlAND (but I think you want an OR on the F1 and G1 values).

The changes in bold below should make the code work:

Sub VV()
Dim a As String, b As String
a = Sheets("LTSB").Range("F1").Value 'Change to cell of your choice
b = Sheets("LTSB").Range("G1").Value 'Change to cell of your choice
Sheets("LTSB").ListObjects("Tabel1").Range.AutoFilter Field:=1, Criteria1:= _
 a, Operator:=xlOr, Criteria2:=b
End Sub

Addition 1:

To remove the filter, you could add a button whose code includes:

ActiveSheet.ListObjects("Tabel1").AutoFilter.ShowAllData

Addition 2:

If you want to use a 1-D array for the filter criteria, I have since foiund that you can use one with xlFilterValues. In the attached workbook, the module VV has been replaced with this code and will filter the data then remove the filtering after the message:

Sub VV()
'
' Use an array and xlOr for filter criteria
'

Sheets("LTSB").ListObjects("Tabel1").Range.AutoFilter _
 Field:=1, _
 Criteria1:=Array("AA", "BB", "CC"), _
 Operator:=xlFilterValues

MsgBox "Now remove filters..."

ActiveSheet.ListObjects("Tabel1").AutoFilter.ShowAllData

End Sub
Discuss

Discussion

thanks john   it's  better  but  i  would    add another  button  to  show  all data  again,   is  it  poosible?   and  about  xland  when i  can  use it ?
leopard (rep: 88) Nov 19, '20 at 6:52 am
Leopard. See addition to my Answer.

For xlAND, it doesn't apply in your case with F1 and F=G1 but for column D (Value) you could filter numbers so it shows only numbers greater than or equal to 2 and less than 9 as follows:
ActiveSheet.ListObjects("Tabel1").Range.AutoFilter Field:=4, Criteria1:=">=2", Operator:=xlAnd, Criteria2:="<9"


If you want the variables to be in cells with your code, redefine a and b numbers (Short or Long) or As Variant (not String)
John_Ru (rep: 6142) Nov 19, '20 at 7:11 am
thanks  agian  i  have  last  thing  if  i  decide   filtering   about  3 or 4  values   i  put  in  array   but  unfortunatly  it  filters  only  last  value  can  you  correct  me,  please?
this  is  what  i  add in my code 
Dim arr As Variant
arr = Array("AA", "BB", "CC")
Sheets("LTSB").ListObjects("Tabel1").Range.AutoFilter Field:=1, Criteria1:=arr
 
leopard (rep: 88) Nov 19, '20 at 7:19 am
Leopard.

You can't just set a filter criteria as an array (as far as I know) but this works with xlOr (see additions in bold and note that array indexing begins with 0 by default, hence arr(o) for first array element)...

Sub VV()
'
' 
 
'
 
Dim arr As Variant
arr = Array("AA", "BB", "CC")
Sheets("LTSB").ListObjects("Tabel1").Range.AutoFilter Field:=1, Criteria1:=arr(0), Operator:=xlOr, Criteria2:=arr(1), Operator:=xlOr, Criteria1:=arr(2)    '
 
Macro
        MsgBox "Now remove filters..."

        ActiveSheet.ListObjects("Tabel1").AutoFilter.ShowAllData
        
End Sub


Please resist the temptation to extend the question time and time again- if I answered your original question, kindly mark it Selected. Thanks
John_Ru (rep: 6142) Nov 19, '20 at 8:04 am
thanks   john  your  really  answered  my  question     as  about    the  arry  it  gives  me  error  wrong number of argument  or  invalid  property  assignment  if  you  have  any  idea  please  provide  me  solution   
leopard (rep: 88) Nov 19, '20 at 1:02 pm
Leopard, Please see addtion 2 to my answer above- that should work (it does for me)
John_Ru (rep: 6142) Nov 19, '20 at 2:21 pm
many thanks  john   every  thing is  ok 
leopard (rep: 88) Nov 20, '20 at 7:15 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login