VBA FILTER function: SourceArray argument causing Type Mismatch error?

magemaester

New Member
Joined
Dec 17, 2017
Messages
10
Hi guys,

I have a piece of code which requires using the FILTER Function, however the SourceArray argument is giving me a Type mismatch error, any help would be greatly appreciated.

This doesn't work
Code:
Dim bDistinct as Boolean
Dim arrList2() Variant

Redim arrList2(1 to 1559, 1)
arrList2 = Range("A2:A1560").Values

bDistinct = UBound(Filter(SourceArray:=arrList2, Match:="banana", Include:=True, Compare:=vbBinaryCompare)) < 0

This works
Code:
Dim bDistinct as Boolean
Dim arrList2() Variant

Redim arrList2(1)

bDistinct = UBound(Filter(SourceArray:=arrList2, Match:="banana", Include:=True, Compare:=vbBinaryCompare)) < 0

So just from these two examples I guessed that the error is either because

1) ReDim arrList2(1 to 1559, 1) makes this essentially a 2D array which isn't permitted by the SourceArray argument, or
2) Using the arrList2 = Range("A2:A1560").Values turns whatever string I had in the original cells into Numbers, which is also not permitted by the SourceArray

Regarding 1), if I change the ReDim statement to ReDim arrList2(1 to 1559), it still doesn't work. So this just leaves possibility 2), which I don't know how to fix.

If anyone can offer a solution that would be really appreciated. Thanks!!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The Filter function is looking for a 1-D array. Try it this way:
Code:
Dim arrList2 As Variant, bDistinct As Boolean
arrList2 = Application.Transpose(Range("A2:A1560"))
bDistinct = UBound(Filter(SourceArray:=arrList2, Match:="banana", Include:=True, Compare:=vbBinaryCompare)) < 0
 
Upvote 0
Hi JoeMo,

Thanks for the answer. This worked.

Just to provide you with some feedback/clarity on the exact logic of what went wrong :

After tinkering with it for a while, I realised that the FILTER function can ONLY accept an array of horizontal elements as its SourceArray. Therefore, my original code wasn't working even though I had the "ReDim (1 to 1559, 1)" because the following line arrList2 = Range("A2:A1560").Values overrides my ReDim and turns arrList2 into a vertical array of 1559 elements according to the direction of the original data on my spreadsheet. Hence why your transpose worked.
 
Upvote 0
Hi JoeMo,

Thanks for the answer. This worked.

Just to provide you with some feedback/clarity on the exact logic of what went wrong :

After tinkering with it for a while, I realised that the FILTER function can ONLY accept an array of horizontal elements as its SourceArray. Therefore, my original code wasn't working even though I had the "ReDim (1 to 1559, 1)" because the following line arrList2 = Range("A2:A1560").Values overrides my ReDim and turns arrList2 into a vertical array of 1559 elements according to the direction of the original data on my spreadsheet. Hence why your transpose worked.
You are welcome.

Your original code was problematic from the start because, as I said in Post #2 , the Filter function is designed for a 1-D array and your arrList2 is a 2-D array. Using the Transpose turns the vertical range into a 1-D (horizontal) array.
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

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