12

Is there some way of getting an array in Excel of a smaller size than a starting array in a cell worksheet function?

So if I had:

{23, "", 34, 46, "", "16"}

I'd end up with:

{23, 34, 46, 16}

which I could then manipulate with some other function.

Conclusion: If I was to do a lot of these I would definitely use jtolle's UDF comb solution. The formula that PPC uses is close, but diving in and testing, I found it gives errors in the empty slots, misses the first value, and there is an easier way to get the row numbers, so here is my final solution:

=IFERROR(INDEX($A$1:$A$6, SMALL(IF(($A$1:$A$6<>""),ROW($A$1:$A$6)),ROW(1:6))),"")

Which must be entered as an array formula (CTRL-SHIFT-ENTER). If being displayed then it must be entered in at least an area as big as the resultset to show all results.

3
  • 2
    I'm going to say it's not possible without a UDF. I'll be very pleased if someone proves me wrong.
    – Excellll
    Commented Oct 18, 2011 at 23:59
  • Great question. I will flag this to a couple of extreme formula guys, but I agree with Excellll on this
    – brettdj
    Commented Oct 19, 2011 at 22:46
  • @Excellll - a possible formula variation is included below that could also be used inside functions like SUM().
    – lori_m
    Commented Jul 19, 2015 at 14:52

5 Answers 5

10

If all you want to do is grab a subset of an array, and you already know the positions of the elements you want, you can just use INDEX with an array for the index argument. That is:

=INDEX({11,22,33,44,55},{2,3,5})

returns {22,33,55}. But that's usually not very useful because you don't know the positions, and I don't know any way to get them without a UDF.

What I have done for this kind of in-worksheet array filtration is to write a UDF with the following form:

'Filters an input sequence based on a second "comb" sequence.
'Non-False-equivalent, non-error values in the comb represent the positions of elements
'to be kept.
Public Function combSeq(seqToComb, seqOfCombValues)

    'various library calls to work with 1xn or nx1 arrays or ranges as well as 1-D arrays

    'iterate the "comb" and collect positions of keeper elements

    'create a new array of the right length and copy in the keeper elements

End Function

I only posted pseudocode because my actual code is all calls to library functions, including the collect-positions and copy-from-positions operations. It would probably obscure the basic idea, which is pretty simple.

You'd call such a UDF like so:

=combSeq({23, "", 34, 46, "", "16"}, {23, "", 34, 46, "", "16"} <> "")

or

=combSeq(Q1:Q42, SIN(Z1:Z42) > 0.5)

and use Excel's normal array mechanics to generate the "comb". It's a lightweight, Excel-friendly way to get a lot of the benefits of the more standard filter(list-to-filter, test-function) function you might see in other programming systems.

I use the name "comb" because "filter" usually means "filter with this function", and with Excel you have to apply the test function before calling the filtration function. Also it can be useful to compute one "comb" as an intermediate result and then use it to...er, comb...multiple lists.

6
  • I was figuring it would have to be something along these lines. Thanks for the complete answer. Commented Oct 19, 2011 at 2:33
  • @jtolle, i wish your INDEX({11,22,33,44,55},{2,3,5}) solution worked, but for me it only returns the first value, 22. It does not return an array. Have you verified it works? Am i doing it wrong? thx
    – johny why
    Commented Sep 10, 2014 at 18:35
  • @johnywhy, have you entered it as an array formula, that is, with "Ctrl-Shift-Enter"? You also have to have a range selected that is big enough to return the array, or it will only display as much as there is room for. Do a search on "excel array formula" for tons more information about this.
    – jtolle
    Commented Sep 11, 2014 at 2:23
  • @jtolle, finally getting back to this question. johny why is correct that it only returns 22, in all 3 positions, entered as an array formula, and in the proper sized range. I think your UDF solution is probably best long term, so I'm marking it as the accepted answer. My conclusion has been edited into my question. Commented Mar 5, 2015 at 16:41
  • 2
    @Lance, Aha, I need to qualify further. In the =INDEX({11,22,33,44,55},{2,3,5}) case the range you put the formula in has to be a horizontal array. If you put that in a vertical array, you'll just get a repeated 22 unless you use semicolons instead, i.e. =INDEX({11;22;33;44;55},{2;3;5}).
    – jtolle
    Commented Mar 5, 2015 at 23:38
5

There is an answer on this site: http://www.mrexcel.com/forum/showthread.php?t=112002. Not much explanation though.

Assuming you have data with blank cells on column A and you put this in column B; that will retrieve data in the same order skipping the blanks

=INDEX(  $A$1:$A$6, 
         SMALL(  
            IF(
               ($A$2:$A$6<>""), 
               ROW($A$2:$A$6)
            ), 
         ROW()-ROW($B$1)
         )
      )

Here is the explanation:

  • ROW()-ROW($B$1) is just a trick that will give you an incrementing number (ie 1 in B1, 2 in B2...)
  • IF (... , ROW($A$2:$A$6) ) is the main part of the trick: it builds an array of the row numbers where the IF condition is true (note that the IF has no 'else' value)
  • SMALL(..) will return the Xth smallest value of that array (in our case the number of the Xth nonblank row), where X is the row number of the current cell (1 in B1 ...)
  • INDEX will then translate from the row number to its value
  • Note that INDEX and ROW start one row above the actual table to always have an offset > 0 (INDEX does not like zeros)
4
  • ROW() - ROW($B$1) can also be written as ROWS($B$1:B1). Another trick, same job
    – PPC
    Commented Apr 3, 2012 at 20:14
  • Nice! If only Excel would include some kind of filter function built-in, this kind of formula gymnastics wouldn't be necessary...
    – jtolle
    Commented May 27, 2012 at 5:37
  • @jtolle: In some cases you can add your custom formulas as macros. By "In some cases", I mean "if your customers don't freak out when they hear the word"
    – PPC
    Commented Jun 28, 2012 at 19:11
  • @PPC, finally got back to this question and gave my conclusion, based on your answer, in my question. Commented Mar 5, 2015 at 16:40
2

The above answers all give brittle formulas that cannot be moved to different locations on the sheet and are very sensitive to inserted rows and columns.

Here is a version that is not sensitive and can be moved around to any row:

=INDEX($A$10:$A$40, SMALL(IF(B$10:B$40,ROW(INDIRECT("1:30"))),ROW(INDIRECT("1:30"))))

In this example the original array values are placed in $A$10:$A$40 (perhaps by using the array formula {TRANSPOSE(originalArray)} if the original data was a row instead of a column).

Column B$10:B$40 contains boolean flags (TRUE or FALSE) that determine if this array element should be preserved in the result (TRUE) or not (FALSE). You can populate this column using any function you want. To create the test mentioned in the OP, <>"", B$10 should be filled with: =A10<>"" (and then copied down thru B$40). Column A has absolute column references and column B has relative column references, so the formula can be copied over into columns further to the right, allowing you to create other types of attributes and sub-arrays, which will be governed by boolean tests you put in columns C and D etc.

This example will handle an original array of up to 30 elements. For a larger array, adjust the ranges $A$10:$A$40 and B$10:B$40 (which represent 30 rows) and also adjust the two occurrences of "1:30" to suit.

1

A possible worksheet function solution:

=INDEX(A1:A6,N(IF(1,MODE.MULT(IF(A1:A6<>"",ROW(1:6)*{1,1})))))

The MODE.MULT function returns a reduced array of indices and N(IF(1,.)) is inserted so that the array is passed by-reference to the INDEX function.

2
  • This looks very interesting. One issue so far is that even wrapping this in an IFERROR statement, like my solution's INDEX, I still get the #N/A errors on the blanks. I'd certainly love a good explanation of how this construction is working, a link would be fine. Commented Jul 20, 2015 at 15:16
  • To array enter over multiple cells maybe try like this: IFERROR(IF(1,<formula>),""). Googling for Excel "N(IF(1," returns a few links but I haven't found a good explanation for this exact behaviour, translating some of the Chinese posts may also give some extra insight.
    – lori_m
    Commented Jul 20, 2015 at 21:13
1

Here is how to do it in the 2D array case.

Using an array address of "G9:I11" and selecting columns 1 and 3 only.

=DROP(REDUCE("",{1,3},LAMBDA(a,b,HSTACK(a,INDEX(G9:I11,,b)))),,1)

This way you can take individual columns from an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.