简体   繁体   中英

Change INDEX MATCH formula to an array formula in EXCEL

I am trying to create an INDEX MATCH formula that searches through a column containing a list of jpegs and returns all jpegs that start with a particular string and converts them to a hyperlink.

Currently my formula returns just the first instance, but I'd like it to return all matches.

The list of jpegs is in column F (F1:F1000) on Sheet 2 of the workbook. The string that is being used in the search is the product SKU in column A, sheet 1 .

Here is the working non-array version which I have entered in C2 on sheet 1 and filled down:

=IFERROR(
    HYPERLINK(
         CONCATENATE(sku_url,INDEX(Sheet2!$F$1:$F$1000,
             MATCH(A2&"*",Sheet2!$F$1:$F$1000,0),1))),
    "image not found")

This works for column C , but how can I fill this formula to the right so that column D contains the second image for each sku, E contains the third, and so forth. I plan to have no more than six images for each SKU, so I have assigned columns C through H to product image URLs. If a SKU doesn't have six images, these extra columns should be empty.

Assuming use of Excel 2010 or later:

=IF(COLUMNS($A:A)>COUNTIF(Sheet2!$F$1:$F$1000,$A2&"*"),"",IFERROR(HYPERLINK(CONCATENATE(sku_url,INDEX(Sheet2!$F:$F,AGGREGATE(15,6,ROW(Sheet2!$F$1:$F$1000)/(LEFT(Sheet2!$F$1:$F$1000,LEN($A2))=$A2),COLUMNS($A:A))))),"imagenotfound"))

As way of an explanation, the initial IF clause, ie:

IF(COLUMNS($A:A)>COUNTIF(Sheet2!$F$1:$F$1000,$A2&"*"),""

is straightforward enough:

COUNTIF(Sheet2!$F$1:$F$1000,$A2&"*")

simply gives a count of the total number of rows which match that condition, and since:

COLUMNS($A:A)

which is equal to 1 and becomes, when copied to the right, successively:

COLUMNS($A:B)

(which is equal to 2)

COLUMNS($A:C)

(which is equal to 3)

etc., etc., this clause will be equivalent to, in successive columns:

IF(1>COUNTIF(Sheet2!$F$1:$F$1000,$A2&"*"),""

IF(2>COUNTIF(Sheet2!$F$1:$F$1000,$A2&"*"),""

IF(3>COUNTIF(Sheet2!$F$1:$F$1000,$A2&"*"),""

etc., etc., and so a blank will be returned in cells where that initial clause is TRUE .

The only other clause of note is that which generates an array of successive row numbers for when this condition is met. Unfortunately the COUNTIF statement above is, for technical reasons, not employable within our AGGREGATE construction.

Fortunately we can reproduce the results of that COUNTIF statement using another set-up with LEFT .

Reducing the range in question temporarily from F1:F1000 to F1:F10 to aid the explanation, this part:

LEFT(Sheet2!$F$1:$F$10,LEN($A2))=$A2

will simply generate an array of Boolean TRUE / FALSE returns as to the result of that statement for each of the entries in F1:F10. We might have, for example:

{FALSE;TRUE;FALSE;TRUE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE}

When we then reciprocate the equivalent row numbers for each of those entries with this array of Booleans, ie perform:

ROW(Sheet2!$F$1:$F$10)/(LEFT(Sheet2!$F$1:$F$10,LEN($A2))=$A2)

we have:

{1;2;3;4;5;6;7;8;9;10}/{FALSE;TRUE;FALSE;TRUE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE}

and since, when coerced by any suitable mathematical operation (of which division is one), Boolean TRUE / FALSE values are coerced into their numerical equivalents ( TRUE =1, FALSE =0), the above becomes:

{#DIV/0!;2;#DIV/0!;4;5;#DIV/0!;7;#DIV/0!;9;#DIV/0!}

Since AGGREGATE , with a first parameter of 15 is instructed to find the smallest value within an array, and with a second parameter of 6 is instructed to ignore any error values within that array, all that is left is to set the fourth parameter within that function, k , which determines whether the first smallest, second smallest, etc. value should be returned.

Again, by using:

COLUMNS($A:A)

for this parameter, which we know will generate a series of consecutive integers (1, 2, 3, etc.) as copied to the right, we thus guarantee that we will return the required row number to each version of the formula.

Regards

Use the AGGREGATE¹ function with the SMALL sub-function ( 15 ). Adjust the k parameter to increase with COLUMN as you fill right.

The standard (non-array) formula in B2 is,

=IFERROR(
    HYPERLINK(
        CONCATENATE(sku_url, INDEX(Sheet2!$F:$F,
            AGGREGATE(15, 6, ROW($1:$999)/(LEFT(Sheet2!$F$1:$F$999, LEN($A2))=$A2), COLUMN(A:A))))),
    "image not found")

Fill right as necessary.

aggregate_first_Second


¹ The AGGREGATE function was introduced with Excel 2010. It is not available in earlier versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM