Programmatically filling Excel column with all possible sums of two value combinations - excel

I'm trying to fill a column with the number of calculations depending on a number provided as an argument. For example, if I plug in that I need to buy 50 widgets from two possible widget types:
Widget 1: $1.00
Widget 2: $1.15
I would like to be able to fill a column with 50 different values, each being the total price for 50 "widget 1"s, the total price for 49 "widget 1"s and 1 "widget 2", etc. Here's an example showing a spreadsheet for the total price for the first possible combination:
The idea being that just by changing B4/X in the spreadsheet, I can get the sum for each combination of widgets for X widgets total, with each sum being in its own cell. I have the beginnings of a formula to calculate this that seems like it might work for iterations of the different widget 1/2 mixes, but the B4 value is hardcoded and I also don't know how to carry the formula down column C the same number of times as the value in B4. Is VBA likely my best bet here?
=((50+0)*B2)+((50-50)*B3)

You may use the ROW() function like this:
=IF(OR(ROW()-ROW($C$2)<0,$B$4<ROW()-ROW($C$2)),"",(($B$4-(ROW()-ROW($C$2)))*$B$2+(ROW()-ROW($C$2))*$B$3))
You have to pull it down as long as the maximum possible value of B4. If B4's actual value is smaller than the maximum, the formula will result in empty strings.

Similar to z32a7ul's answer, but maybe a little shorter:
=IF($B$4+2-ROW()>=0,($B$4+2-ROW())*$B$2+(ROW()-2)*$B$3,"Out of Bounds")
The formula 1) checks if you exceeded the number of possible combinations of the widget summation and 2) pastes the sum if you haven't or 3) writes "out of bounds" if you have. Start in row 2 (e.g. cell C2) and pull down.
The formula assumes a) that you start in row 2 (otherwise, change every 2 in the formula to the row in which you start).
The widgets prices are in cells B2 and B3 (otherwise change the reference, and keep the $ signs, so when dragging the formula down, the references are kept)
The number of combinations is in B4 (again, adjust if necessary)

Related

Excel Sum X amount of columns depending on a value in another cell

Is there a way that you can use sum() in excel and that sums up x amount of columns depending on number given in another cell.
See below for example
Example of how it's today
In this picture I have columns B:M were I put in Actual figures.
And in columns T:AE I have budget figures.
In this example I have actual figures JAN-MAY and would like to compare them vs budget JAN-MAY.
How I do today is that I go in to column AF and drag so it's =SUM(T6:X6) so it only takes T:X in this case.
But I would like that cell AF has a formula in it were it looks at cell AH:3 and sees that it's number 5.
Therefore AF should sum(T;U;V;W;X(5columns))
Hope you understand my problem and have a solution!! :)
You can use the OFFSET function to determine the number of columns to SUM. In your example, AH3 is your offset amount.
In AF6 you can place this formula:
=SUM(T6:OFFSET(T6,0,AH3-1))
Which breaks down to
=SUM(T6:OFFSET(Start at T6, Offset 0 rows, Offset columns the value of AH3 and subtract one for a total of five columns))

Copy and Paste an array of cells with information dependent on a lock cell (from a reference cell) with relative value

I am having the following problem:
I have a list of items number and descriptions starting at A5 and going down, at B5 I have the total sold quantity over a year, C5 corresponds to sales price per unit and D5 to its COGS per unit.
Further down, starting in cell A12 I have a specific item number and its description. I have then the quantity sold of this item each month (cells B13:D13) and underneath I want to have the sales quantity per month times the unit sales price of that item (that is in cells B14:D14).
To do so, from B14 I look up the item number and description in A12, in the list of items I mentioned before ($A$5:$D$7) in order to get the price for that item and then multiply it by that months sold quantity.
The problem with this approach is that when I drag that result along, over the following months, it stops searching the value at A12 and instead looks for the item value at B12, then C12 etc etc.
So in order to fix that I locked the value at A12 using the formula '=B13*VLOOKUP(INDIRECT(ADDRESS(ROW(Artikel)+ROWS(Artikel)-4;COLUMN(Artikel)+COLUMNS(Artikel)-4);TRUE);ItemSales;3;FALSE). Thanks to that, I am able to drag the result of the first month over the next months and to get a right result.
However, I would like to copy the table with the monthly results ( $B$13:$D$15 ) and paste it some rows bellows, and that the item referenced is not anymore the previous one at A12, but a new one let’s say in A26.
I achieve that with the formula k=VLOOKUP(OFFSET(INDIRECT(ADDRESS(ROW();COLUMN()));-2;-1;1;1);ItemSales;3;FALSE)*B27 but this formula doesn’t hold the item number and description value when dragged along, so I don’t get to combine them both
Any ideas how I can make it happen??
I have designed two formulas for you. The first one is for column A. Enter it in A12 and copy down, 6 rows for each row in your Item_Sales range.
=IF(MOD(ROW(),6)=0,INDEX(Item_Sales,INT((ROW()-12)/6)+1,1),IFERROR(INDEX({"Total quantity","Total Sales","Total COGS"},MOD(ROW(),6)),""))
Note that the number 12, wherever it occurs in the formula refers to row 12, where your named range "Artikel" starts. It's the first row where the first item in "Item_Sales" must appear. The number 6 refers to the number of rows in one data set, essentially Rows(12:17) for the first set, Rows(18:23) for the second etc. Your row 16 is hidden, row 17 blank. If you want the formula to write anything there expand the array {"Total quantity","Total Sales","Total COGS"}
The other formula is for cell B13. From there you can copy it to B14:B15. You can also copy B13:B15 to B19 but in its present form the formula will throw an error in B16:B18.
=INDEX(Item_Sales,INT((ROW()-12)/6)+1,MOD(ROW(),6)+1)*IF(ISNUMBER(B12),B12,1)
Again, the meaning of numbers 12 and 6 is as explained above. You would need to modify them if your data or display format changes in the future.
For your better understanding, I used the INDEX function which takes a 2D range and extracts values from it based on coordinates. INDEX(Item_Sales, 1, 2) returns the value of the cell in the first row, second column of the range defined as "Item_Sales". Of course, these numbers can be calculated.
The other functions I use are INT() and MOD(). Int(5/6) returns 0, as does INT(1/6). This function can be used to locate the first row in a set the repeats every 6 rows. INT(13/6) returns 2, INT(19/6)=3. These calculations identify the row numbers in Item_Sales referenced in Artikel.
MOD returns the modulus of a division. MOD(5, 6) returns 5, MOD(13,6) returns 1. If there is no modulus the return is 0. With the help of this information you can repetitively count from 0 to 6 and restart with 0.

IF all cells in a range are the same, excluding blank cells

I have a table to calculate the price of a project, where one column is the description of an element of the project followed by a column for its unit (usually hours), the price per unit, number of units, and the final price,
sort of like this:
My question really only involves the highlighted cells. Im making a template, and I add elements to the table as I go along, so some rows are left blank, but I want the total number of hours to be displayed in D7 (easy enough, =SUM(D2:D6)), but my problem is that some of the elements aren't written per hour (eg. row 4), so I want the total to only show up if all the values are for hours. Essentially:
IF all values in row B are "Hours", then sum of row D, else "")
I guess in short, is there a formula I can use that would return TRUE if all the values in a range are the same, excluding blank cells?
Thanks.
As per your last comment, you can try:
Formula in D7:
=IF(COUNTIFS(B2:B6,"<>Hours",B2:B6,"*")=0,SUM(D2:D6),"")

How to find the maximum value of a given range, dependent on the value in a separate column

Screenshot of the Excel worksheet
I'm working with historic stock prices, and using eight columns I have:
Column A: High
Column B: Low
Column C: Close
Column D: Cx-Cx-4
Column E: Counts the number of consecutive positive numbers in column D
Column F: Counts the number of consecutive negative numbers in column D
Column G: Calculate the difference between the maximum of column A and minimum of column B within a given sequence.
As an example G1 should equal:
=max(A1:A5)-min(B1:B5)
G6 should equal:
=max(A6:A8)-min(B6:B8)
G9 should equal:
=max(A9:A11)-min(B9:B11)
And so on.
I'd like to know if it is possible to automate this calculation, possibly with the use of one or more additional columns.
Welcome to SO!
This may not be the most efficient solution as you need to add two helper columns, but if I understand your requirements correctly, then this idea should work well enough.
First, let's assume that there are 100 rows in your data set. Given that, enter the formula "=A100" in cell G100 and the formula "=B100" in cell H100. This sets up the boundary condition for the formulas in columns G and H. Now, in cell G99, enter this formula:
"=IF(E99="",G100,IF(E100="",A99,MAX(A99,G100)))"
What this formula does is set up a "running maximum" with the following logic:
If the cell in E99 is blank, copy the running maximum from G100, else:
If the cell in E99 is not blank but the cell in E100 is, set up a new running maximum from the cell in A99, else:
Take the maximum of A99 and G100 as the new running maximum.
Similarly, copy the following formula into cell H100:
"=IF(F99="",H100,IF(F100="",B99,MIN(B99,H100)))"
This follows the same logic as the previous formula, but takes the minimum of column B.
Copy or autofill these formulas to the top of the data set. This should now give you running maximum for column A and a running minimum for column B.
The next step is to calculate the difference. I notice from your question, that you only seem to be interested in calculating this difference at the top of each range (G1, G6, G9, etc.), rather than doing it in every row. Given that, we need a slightly more complicated formula.
The boundary condition for this formula is simply "=G1-H1" entered in cell I1. In cell I2, enter this:
"=IF(OR(AND(E2<>"",E1=""),AND(F2<>"",F1="")),G2-H2,"")"
How this works is that it check two conditions that indicate a range boundary:
E1 is blank and E2 is not
or
F1 is blank and F2 is not
If either of these conditions hold, the IF statement is true and "G2-H2" is diplayed, otherwise a blank cell is displayed. Now copy or autofill this formula to the bottom of the data set.
As a final step, you can now hide columns G and H if you don't need them displayed. This should now give you the results I think you're looking for. Please let me know if this doesn't work out for you.

Excel: Multiply each cell in row X by corresponding cell in row 2, and get sum

I have a table set up as follows:
Column 1 - Customer Name
Row 1 - Item Name
Row 2 - Item Cost
Row 3+- Item Quantity
How do I set up the last column to calculate the total cost for each customer? I.e, For each customer row, I want to multiply the number in each cell (= quantity) by the corresponding cell in Row 2 (= cost), and add them all up for the final bill.
To clarify what I'm saying I'm attaching the following picture so that we can discuss specifics.
Have you tried SUMPRODUCT - it does exactly what you need, gives the sum of 2 or more multiplied ranges?
=SUMPRODUCT(A71:C71,$A$2:$C$2)
You can extend the ranges as far as you need. If you want to add columns make sure you don't add at the end, e.g. if you retain one blank column (D currently) and include that in the formula, then if you add a column at D the formula will automatically extend to E
You can use sumproduct but specify the ranges, e.g. =sumproduct(B2:B6,C2:C6), the next row would then be =sumproduct(B2:B6,D2:D6) etc. I'm sure there's a way to "fix" your cost row but it's quite quick doing it this way
If, for example, your first data set is in column A (i.e. per unit cost) and the second data set is in column B (i.e. quantity), and you want the total cost for each item for the specified quantity, place the following formula in C1
=A1*B1
Select C1 and drag the fill handle - this is the small
black square at the bottom right corner of the cursor as far down the column as you need. The program will automatically replicate the formula with the correct cell numbers for each row.
One way is to use this formula:
=SUM(B4:B5)*B2+SUM(C4:C5)*C2
It is not so cool but you still need to expand the formula even with SUMPRODUCT because the range has to be the same as far as I know.
The other way I came up will use a matrix function called MMULT and here is the example:
With this array (means you have to click Ctrl + Shift + Enter altogether) formula entered into cell D6: =SUM(MMULT(B2:C2,TRANSPOSE(B3:C5))), you will get your expected result without needing all the subtotals. Please note this is a 2 x 1 By 2 x 3 Matrixformula.

Resources