Share via

How to INDEX sparse array?

Anonymous
2010-05-10T17:56:56+00:00

How can I INDEX a sparse array without resorting to VBA?

By "sparse", I mean a mix of elements with FALSE values and other values.  I want to ignore the FALSE values when applying the index.

The situation arises in the following array formula, for example (assume C1>=2):

=INDEX(IF(A1:A10=1,B1:B10),INT(C1/2))

where C1 is the array formula =COUNT(IF(A1:A10=1,B1:B10)).

In actual usage, I do not always want C1/2.  And the selection criteria for the array might not always be one simple criterion.

To clarify, I want 3 from the following formula:

=INDEX({1,FALSE,3,4},2)

The following VBA function demonstrates the desired behavior:

Function myIndex(a, n)

Dim i As Long, k As Long

myIndex = CVErr(xlErrNum)

If TypeName(a) = "Variant()" Then

   'If UBound(a, 2) <> 1 Then exit function

   For i = 1 To UBound(a, 1)

      If a(i) <> False Then

         k = k + 1

         If k = n Then myIndex = a(i): Exit Function

      End If

   Next

End If

End Function

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more