Filtering array

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,074
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Dim diff%()
Dim Result%()

Diff is conditionally filled with 1
But retrieving them gives Type Mismatch error

Result = Filter(diff%, 1)
or
Result = Filter(diff%, "1")
What an I doing wrong?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The VBA Filter function expects a String array.

You have declared diff and Result as Integer.

If you need further assistance, it would help if you posted the rest of the relevant code.
 
Upvote 0
I changed from integer to string.
Assume it's not possible to filter an integer array?
 
Upvote 0
It was an integer array filled with 0's by default, and 1 conditionally.
I wanted a count of how many 1's. Filter would give me the Ubound value.
But I have it working now after changing the datatype.
 
Upvote 0
It is not answering your question about filtering the array (that has been answered) but in the particular circumstances you describe, you could leave the Diff array as Integer and use this to get the number of 1s
VBA Code:
Application.Sum(diff%)
 
Upvote 0
Solution
... or, if your code is looping through the array to populate the 1's, you could set up a counter variable, adding 1 to the tally every time a 1 is set.
 
Upvote 0
Hi Peter,

Sorry to hijack the post but can I ask why this errors out (Type mismatch):

VBA Code:
Application.WorksheetFunction.CountIf(diff, 1)

Thanks,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,121
Messages
6,176,496
Members
452,733
Latest member
Gao87

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