简体   繁体   中英

Excel - Understanding a SUMPRODUCT formula

I have this excel formula that someone has created without documentation and I'm struggling to understand it.

=SUMPRODUCT(-MID(TEXT(MID(TEXT(F2,REPT(0,15)),ROW(INDIRECT("1:15")),1)*{2;1;2;1;2;1;2;1;2;1;2;1;2;1;2},"00"),{1,2},1))

it looks like it creates a "random" digit from another number. There are few key things I'm stuggling with:
* why is there an array ( {1,2} ) given to a MID() function?
* since there is a SUMPRODUCT() , which needs an array, I'm assuming that the result of the -MID() function is some sort of an array, how do I see what it is?
* what does multplying by an array {2;1;2;1;2;1;2;1;2;1;2;1;2;1;2} does?
* the INDIRECT() functions seems to always return 1?

any help would be appriciated.

There is a function in EXCEL called Evaluate Formula , a good tool to check the formula step by step.

Assuming F2 is 123

  1. REPT(0,15)

    Generate a string with 15 "0", that is "000000000000000"

  2. TEXT(F2,[1])

    Convert F2 into a string with 15 char. Eg. 123 > "000000000000123"

  3. ROW(INDIRECT("1:15"))

    Return an array {1;2;3;4;5;6;7;8;9;10;11;12;13;14;15}

  4. MID([2],[3],1)

    Separate [2] into an array, each element is a char {"0";"0";"0";"0";"0";"0";"0";"0";"0";"0";"0";"0";"1";"2";"3"}

  5. [4]*{2;1;2;1;2;1;2;1;2;1;2;1;2;1;2}

    Since {A;B} * {C;D} = {A*C;B*D}

    {"0";"0";"0";"0";"0";"0";"0";"0";"0";"0";"0";"0";"1";"2";"3"}*{2;1;2;1;2;1;2;1;2;1;2;1;2;1;2} ={0*2;0*1;0*2;....}={0;0;0;0;0;0;0;0;0;0;0;0;2;2;6}

  6. TEXT([5],"00")

    Converts the elements in the Array to two char by adding "0" in the front. The array goes to {"00";"00";"00";"00";"00";"00";"00";"00";"00";"00";"00";"00";"02";"02";"06"}

  7. MID([6],{1,2},1)

    Note that {A,B} and {A;B} are different. {A,B} is an array with 1 row and 2 columns; {A;B} is an array with 2 rows and 1 column.

    In this formula, you can imagine doing MID twice, first time we use 1 as second parameter, second time we use 2 instead.

    The result is a 2-D array: {"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","0";"0","2";"0","2";"0","6"}

  8. SUMPRODUCT(-[7])

    The minus sign before [7] will force all elements in the array to convert to numbers with opposite sign. In this example, it sums 0+0+0+...+(-2)+0+(-2)+0+(-6) = -10

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