Filtering array

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,953
Office Version
  1. 2019
  2. 2016
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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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
Thanks. Always good to ask as so many new things to learn.
 
Upvote 0
... 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,221,525
Messages
6,160,327
Members
451,637
Latest member
hvp2262

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