FILTER function

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,825
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to use the Filter function but get an error.

Code:
    Dim rng As Range

    Set rng = Sheet1.Range("A1:A100")
    
    Dim DataArray() As Variant
    
    DataArray = rng

    Dim Output As Variant
    
    Output = Filter(DataArray(), "Apples", True)

the error message is:

Code:
Run-time error '13'

Type mismatch

Is it because DataArray needs to be a String instead of a Variant?

I tried this but it still failed:

Code:
    Dim rng As Range

    Set rng = Sheet1.Range("A1:A100")
    
    Dim DataArray() As Variant
    
    DataArray = rng
    
    Dim OutputArray() As String
    
    ReDim OutputArray(1 To 100, 1 To 1) As String
    
    Dim i As Integer
    
    For i = 1 To 100
    
        OutputArray(i, 1) = DataArray(i, 1)
    
    Next i
    
    Dim Output As Variant
    
    Output = Filter(OutputArray(), "Apples", True)

with the same error message as before.

Can someone please tell me what is wrong?

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Filter only works with a 1D array, but you are giving it a 2D array. Try
VBA Code:
    DataArray = Application.Transpose(rng)
 
Upvote 0
Filter only works with a 1D array, but you are giving it a 2D array. Try
VBA Code:
    DataArray = Application.Transpose(rng)
Thanks, you just beat me to it.

I tired this and it worked.

Code:
    Dim rng As Range

    Set rng = Sheet1.Range("A1:A100")
  
    Dim DataArray() As Variant
  
    DataArray = rng
  
    Dim OutputArray() As String
  
    ReDim OutputArray(1 To 100) As String
  
    Dim i As Integer
  
    For i = 1 To 100
  
        OutputArray(i) = DataArray(i, 1)
  
    Next i
  
    Dim Output As Variant
  
    Output = Filter(OutputArray(), "Apples", True)
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

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