Excel - Writing a Complex Formula - excel

I have the following numbers in the range AI3:AJ41:
34 3
26 3
25 3
24 2
24 2
24 2
24 2
24 2
24 2
24 2
24 2
24 2
22 2
22 2
22 2
22 2
21 2
21 2
21 2
21 2
19 2
19 2
19 2
19 2
19 2
19 2
19 2
19 2
17 2
17 2
17 2
15 2
15 2
15 2
15 2
12 1
12 1
12 1
12 1
The AI column contains values while the AJ column contains the following formula in cell AJ3:
=ROUNDUP(AI3/12,0)
which spans down to AI41.
In cell AJ2 I have the following sum formula
=SUM(AJ3:AJ41)
which results in 77.
I want to write a formula into cell AI2, to get the same result as I now have in AJ2 without using the (helper) column AJ i.e. only using the values of the AI column.
Maybe it can be a VB macro to do is, i don't know...
I've provided a link to my Sample File for easy copy/pasting.
Thank you for your time & help.

Updated November 27
I'm still at an airport, and decided to do this problem a stab without VBA/UDF.
An Array function can get you what you want. Type this into your worksheet with CTL SHIFT ENTER for a non-vba solution:
=SUM(ROUNDUP(COUNTIF(A1:AH1,">="&ROW(INDIRECT("A1:A"&MAX(A1:AH1),TRUE)))/12,0))
You can download XLSM file here.
For anyone who ever wishes they could perform VBA "loops" in a regular Excel formula, this is a pretty good example of how to approach as this essentially "loops" through the range with a higher integer each time by using the Row and Indirect functions. Chip Pearson's sight is brilliant for stuff like this.
UPDATED Nov 26
I am sitting bored in an airport, so I will give this another go... I think this custom function will get the OP what they want. Put this in anywhere and you'll get 77. =UMutCustom2(A1:AH1)
The custom function code needed is here:
Function UMutCustom2(rng As Range) As Double
Dim r As Long
For r = 1 To Application.WorksheetFunction.Max(rng)
UMutCustom2 = Application.WorksheetFunction.RoundUp(Application.WorksheetFunction.CountIf(rng, ">=" & r) / 12, 0) + UMutCustom2
Next r
End Function
original Answer
I included both of these examples in a file here.
Probably easiest to use an array formula. In cell Ai1 put this formula: =SUM(ROUNDUP(AI3:AI999/12,0))
However, after typing the formula YOU MUST HIT CTR SHIFT ENTER!
This will create curly brackets around the formula so when you view the formula it should appear: {=SUM(ROUNDUP(AI3:AI999/12,0))} and does sum to 77 on my version of file.
(good news is that in 2019 Excel's new query engine will not require the CTL SHIFT!)
Alternatively, if you want to make a custom function using vba, you could use this custom function, that would not require ctr shift enter... here's VBA code to make that:
Function UMuTCustomFunc(rng As Range) As Double
Dim ws As Worksheet, rCell As Range
Set ws = Sheets(rng.Parent.Name)
For Each rCell In Intersect(ws.UsedRange, rng).Cells
UMuTCustomFunc = Application.WorksheetFunction.RoundUp(rCell.Value / 12, 0) + UMuTCustomFunc
Next rCell
End Function

Sumproduct
If you don't want to use an array formula (too lazy to press CTRL SHIFT ENTER) you can use:
=SUMPRODUCT(ROUNDUP(AI$3:AI$41/12,0))
and press ENTER. The result is 77.
Funny
I didn't even know there was a ROUNDUP function so I used this at first:
=SUMPRODUCT(IF(MOD(AI3:AI41,12)=0,INT(AI3:AI41/12),INT(AI3:AI41/12)+1))
but it doesn't work without entering it as an array formula. This should serve as a reminder that even SUMPRODUCT won't always work as a 'non-array' formula.
Addition
If you want to apply the same principle to the range A1:AH1, use this formula:
=SUMPRODUCT(ROUNDUP($A1:$AH1/12,0))
The result is 87 though.

Related

In spreadsheet (Excel or Google Sheets), how to find the sum of multiple values with multiple rows of column headers criteria?

as in the above picture, I need to sum all the values within the table C8:V22 which match
the first three characters of column B(orange),
row 5 for the number(blue),
row 6 for the first character(green),
row 7 for the first three characters(yellow)
The desire result would be the sum of the number in red, which is 10001 + 10081 + 10161 + 10221 = 40464
I tried many different ways to write the formula, one of that is:
=INDEX($B$5:$V$22,
MATCH($D$26,LEFT($B$5:$B$90,3),0),
MATCH(($F$26=$5:$5) * ($G$26=LEFT($6:$6, 1)) * (H26=LEFT($7:$7,3)), 0))
and pressed Ctrl+Shift+Enter to make it as an array formula, but couldn't figure out where is the error.
Anyone could help on this? Thank you!
Edit: The following is a simplify table for easy reference:
1
2
3
a1
a2
b1
abc1
abdf2
abc2
111a
11
12
13
222a
14
15
16
111b
17
18
19
555a
20
21
22
333d
23
24
25
111a
26
27
28
in this case, the match values are 11 + 17 + 26 = 54
I also tried using combinations of functions such as SUMIFS, SUMPRODUCT, search, e.t.c. but still not able figure out the solution.
In your sheet:
=SUMPRODUCT((LEFT(B8:B22,3)=C26&"")*(C5:V5=E26)*(LEFT(C6:V6,1)=F26&"")*(LEFT(C7:V7,3)=G26&"")*C8:V22)
simply retrieve the sum using your parameters and converting in string to match exactly.
If you have Excel 365 you can use this formula:
=LET(data,C8:V22,
ruleColumnB,LEFT(B8:B22,LEN(D26))=TEXT(D26,"0"),
ruleRow5,C5:V5=F26,
ruleRow6, LEFT(C6:V6,1)=G26,
ruleRow7, LEFT(C7:V7,3) =H26,
SUMPRODUCT(data*ruleColumnB*ruleRow5*ruleRow6*ruleRow7)
)
)
Using LET makes the formula more readable - esp. if you name the rules according to your special context.
If you don't have Excel 365 you have to replace the single parameters within SUMPRODUCT with each range/formula

Excel how to look for value in table and return value from another cell

I have an issue in excel where I cannot get it to return a specific value.
I have a table of data like this
Week N1 N2 N3
w0 6 15 24
w1 5 8 9
w2 3 8 17
w3 20 23 31
w4 13 21 23
w5 6 12 15
w6 2 5 20
w7 10 20 21
the numbers in N1 N2 and N3 can change and are random.
What I need to be able to do is to lookup any number in the table and return the value in the weeks column.
so for example if I was looking for the number 20 it would return w6.
Ive tried various vlookups, Hlookups, Index and Match variations but just dont seem to be able to get this fairly simple thing to work.
Can anybody help me please, before I explode.
Try this:
= INDEX($A$2:$A$9,MATCH(1,MMULT(($B$2:$D$9=20)+0,{1;1;1}),0))
EDIT A slightly better formula:
= INDEX($A$2:$A$9,MATCH(TRUE,MMULT(($B$2:$D$9=20)+0,{1;1;1})>0,0))
It returns the first Week where a match is found in the table.
Obviously just replace 20 above with whatever value you want to look up.
See example below.
EDIT
More generally, instead of hardcoding {1;1;1} into the formula, you can make this dynamic, e.g.
= INDEX($A$2:$A$9,MATCH(1,MMULT(($B$2:$D$9=20)+0,TRANSPOSE((COLUMN($B$1:$D$1)>0)+0)),0))
Also, if you want to search the table from left to right instead of top to bottom, do this:
= INDEX($A$2:$A$9,MATCH(1,MMULT(TRANSPOSE((ROW($A$2:$A$9)>0)+0),($B$2:$D$9=20)+0),0))
Note, both of these are now array formulas (must be entered with Ctrl+Shift+Enter instead of just Enter.
Another method,
=index(A:A, iferror(match(G5, B:B, 0), iferror(match(G5, C:C, 0), match(G5, D:D, 0))))

Lookup multiple values in excel

Is there a way for excel formulas to look up multiple values in an 2d matrix for excel? For example:
sum(vlookup({2015,2016},Matrix,{2,4,6},False)) = 3 + 4 + 5 + 2 + 3 + 2
2014 1 3 7 11 9 2
2015 3 6 4 12 5 8
2016 2 1 3 99 2 6
I don't necessarily have to use the function vlookup but I prefer not having to use the same function multiple times then summing them since the list {2015,2016} could be quite long.
Put your search criteria in one cell each:
Then use this formula:
=SUMPRODUCT(ISNUMBER(SEARCH($A$1:$A$3,J1))*ISNUMBER(SEARCH(COLUMN($B$1:$G$3),K1))*$B$1:$G$3)
If you want to hard code the numbers then you can do it this way:
=SUMPRODUCT(ISNUMBER(SEARCH($A$1:$A$3,"2015,2016"))*ISNUMBER(SEARCH(COLUMN($B$1:$G$3),"2,4,6"))*$B$1:$G$3)
I like #ScottCraner's answer a lot, I just did it slightly differently, but using close-on the same tricks.
For me, I put the year values in as follows in column N:
N1 = 2015
N2 = 2016
And the columns I wanted to use for the look-up in column O:
O1 = 2
O2 = 4
O3 = 6
Then, I used the following (entered as an array formula - meaning you press CTRL+SHIFT+ENTER after putting it in):
=SUMPRODUCT(ISNUMBER(MATCH(A1:A3,$N$1:$N$2,0))*ISNUMBER(MATCH(COLUMN(A1:G3)-COLUMN(A1)+1,$O$1:$O$3,0))*A1:G3)
Again, Scott's answer is perfectly right, I just added in a different way to get to the same result.
Hope it helps you or someone else facing the same kind of challenge.

Excel - Lookup value in multiple columns

I have a datasheet with multiple columns that I want to search through - ending up with returning the top row value(s) in the found column(s). How to do this?
My idea would be to combine index with match, but I am rather lost for specific ideas...
January February March April May June
1 2 3 4 5 6
7 8 9 10 11 12
2 7 1 8 4 5
9 10 11 12 6 3
E.g. Search for: ID 3 - should return: March, June
Regards,
Nyborg
You need to add a VLOOKUP for each columns and after build the result
Following the Scheme:
A7 -> =IF(IFERROR(VLOOKUP($A$9;A1:A5;1;);"")="";"";A1) and autocomplete
D9 -> =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(A7;",";B7;",";C7;",";D7;",";E7;",";F7);",,";",");",,";",");",,";",")
D10 -> =IF(RIGHT(D9;1)=",";LEFT(D9;LEN(D9)-1);D9)
Formula for D9 & D10 it's only to have a good display of the results.
UPDATE Version:
Considering you agree to have the Row 7 ... You can simplyfy using the Result formula:
=INDEX(List;SMALL(IF(List<>"";COLUMN(List)-MIN(COLUMN(List))+1);1))&IFERROR(", " & INDEX(List;SMALL(IF(List<>"";COLUMN(List)-MIN(COLUMN(List))+1);2));"")&IFERROR(", " & INDEX(List;SMALL(IF(List<>"";COLUMN(List)-MIN(COLUMN(List))+1);3));"")
List it's the name Range of Row 7. The formula shall be apply with Ctrl+Shift+Enter.
This formula collect the first 3 value.
If you want more, add for each nth value you have:
&IFERROR(", " & INDEX(List;SMALL(IF(List<>"";COLUMN(List)-MIN(COLUMN(List))+1);3));"")
changing the last numer (in this case 3) and putting 4, 5, 6 ...
Obviusly if the result can be a lot, have few sense... Consider to use VBA !

Run a simulation several times with different parameters and store values

Consider a spreadsheet which performs some computations based on a fixed value (in the rxample below, D3) and is iterative. E.g.
D3: 4
B3: 12
B4: 58 (=B3*$D$3+10)
B5: 242 (=B4*$D$3+10)
B6: 978 (=B5*$D$3+10)
Total = 1290 (=sum(B2:B5))
Now, suppose I wanted to try out different values of D3 (let's call this P) and store the different totals I get, i.e.
P Total
4 1290
5 2252
6 3618
7 5460
How would I do this with Excel? A macro? Please note that the above example is a simplified version of the real thing. It should be clear that I need to compute B3-B6 so I can compute the sum.
Update:
Each computation requires several columns. E.g. we would use values on B3,B4, .. and C3,C4, ... .
A macro can do this. If B7 contains the sum formula, try this
Sub RunSimulation()
Dim p as long
for p = 4 to 7
Range("D3")=p
Debug.Print Range("B7")
Range("L" & (p-1)) = Range("B7").Value
next
End Sub
EDIT: added a line for storing the results, as requested.
If you don't want to enter the sum formula in your sheet, you can calculate the total in VBA either:
Dim total as Long
Dim row as long
total = 0
for row = 2 to 5
total = total + Range("B" & row)
Next
Debug.Print total
(Use Double instead of Long for total if you are dealing with floating point numbers.)
Usually it is done in following manner:
A B C D
1 x tmp1 tmp2 Total
2 3 $A2+10 $B2*10+10 $C2*$B2 The formulae are just for example.
3 $A2+1 $A3+10 $B3*10+10 $C3*$B3
4 $A3+1 $A4+10 $B4*10+10 $C4*$B4
Excel has capabilities to automatically increment indices in formulae.

Resources