Filter function returning zero rows. Assigning results to an array.

HighAndWilder

Well-known Member
Joined
Nov 4, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
Sample code which demonstrates a problem that I am having.

Filter by month on separate tab Version 4.xlsm
HIJKLM
1C9C10C11C12
210539
38345
49641
59621
65369
73845
8
Dashboard2
Cell Formulas
RangeFormula
I1:L1I1="C" & COLUMN()


VBA Code:
Public Sub subFilterData()
Dim arr() As Variant
Dim strFormula As String
Dim intRows As Integer
  
  ActiveWorkbook.Save
  
  ' Works fine, one row filtered.
  strFormula = "FILTER(I2:L7,I2:I7=10,"""")"
    
  ' intRow is assigned a value of 1 which is correct.
  intRows = Evaluate("Rows(" & strFormula & ")")
  
  ' Assign the filter result to an array and a four row one dimensional array is created.
  ' If more than one row is filtered then a two dimensional array is created.
  arr = Evaluate(strFormula)
    
  ' If no rows are filtered.
   strFormula = "FILTER(I2:L7,I2:I7=100,"""")"
  
  ' intRow is assigned a value of 1 which is INCORRECT.
  ' This suggests that executing the next line would be fine.
  intRows = Evaluate("Rows(" & strFormula & ")")
    
  ' Assign the result to an array and an error 13 - Type mismatch is returned.
  arr = Evaluate(strFormula)
  
  ' How does one test to see if the filter returns zero or one row accurately?
  
End Sub
 
Check the below code. If it's working as you expect, you may create a function out of it. or simply add similar check to your real code.

VBA Code:
Public Sub subFilterData()
Dim arr() As Variant
Dim strFormula As String
Dim intRows As Integer
Dim tmp As Variant

'  ActiveWorkbook.Save
  
  ' Works fine, one row filtered.
  strFormula = "FILTER(I2:L7,I2:I7=10,"""")"
  
  tmp = Evaluate(strFormula)
  If VarType(tmp) >= vbArray Then
    
    ' intRow is assigned a value of 1 which is correct.
    intRows = Evaluate("Rows(" & strFormula & ")")

    ' Assign the filter result to an array and a four row one dimensional array is created.
    ' If more than one row is filtered then a two dimensional array is created.
    arr = Evaluate(strFormula)
  Else
    intRows = 0
    MsgBox "Filter returned no data", vbInformation
  End If

  ' If no rows are filtered.
   strFormula = "FILTER(I2:L7,I2:I7=100,"""")"
  
  tmp = Evaluate(strFormula)
  If VarType(tmp) >= vbArray Then
    intRows = Evaluate("Rows(" & strFormula & ")")
    arr = Evaluate(strFormula)
  Else
    intRows = 0
    MsgBox "Filter returned no data", vbInformation
  End If
  
End Sub
 
Upvote 0
Solution
Check the below code. If it's working as you expect, you may create a function out of it. or simply add similar check to your real code.

VBA Code:
Public Sub subFilterData()
Dim arr() As Variant
Dim strFormula As String
Dim intRows As Integer
Dim tmp As Variant

'  ActiveWorkbook.Save
 
  ' Works fine, one row filtered.
  strFormula = "FILTER(I2:L7,I2:I7=10,"""")"
 
  tmp = Evaluate(strFormula)
  If VarType(tmp) >= vbArray Then
   
    ' intRow is assigned a value of 1 which is correct.
    intRows = Evaluate("Rows(" & strFormula & ")")

    ' Assign the filter result to an array and a four row one dimensional array is created.
    ' If more than one row is filtered then a two dimensional array is created.
    arr = Evaluate(strFormula)
  Else
    intRows = 0
    MsgBox "Filter returned no data", vbInformation
  End If

  ' If no rows are filtered.
   strFormula = "FILTER(I2:L7,I2:I7=100,"""")"
 
  tmp = Evaluate(strFormula)
  If VarType(tmp) >= vbArray Then
    intRows = Evaluate("Rows(" & strFormula & ")")
    arr = Evaluate(strFormula)
  Else
    intRows = 0
    MsgBox "Filter returned no data", vbInformation
  End If
 
End Sub
Thanks Kaper

It is the VarType that is the key.

I will create a function as I'll use it quite a lot in the future.
 
Upvote 0
Glad we could help and thanks for marking answer as a solution
 
Upvote 0
FWIW, you could also just declare arr as Variant and then test it with IsArray.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top