Excel solver, variable cell to change adjusts data set - excel

I am new to excel solver and I'm trying use it for what I think fairly simple task but it just won't work.
I have 3 columns as follows:
%CHNG | PnL | Criteria met
1 | 1 | 1
.5 | 2 | 2
2 | -1 | -1
3 | 6 | 6
-1 | .5 |
-.2 | -5 |
and the following 2 cells:
Criteria: 0
Total: =sum of criteria met column
When the number in the 'criteria' cell is changed the value in the 'criteria met' column is posted from the PnL column only if the value in the '%CHNG' column is greater than the value in the 'criteria' cell using an if statement.
The 'total' cell is then the sum of the 'Criteria met' column. (The PnL values that are greater than the criteria value)
I am trying to get solver to maximize the 'Total' cell, by changing the value in the 'criteria' cell, however everytime I try this it leaves the criteria cell unchanged and claims a solution has been found. No matter what value I initially have in the criteria cell the solver claims that is the solution when it is run.
Is there any way to get this to work? preferably without VBA
Thank you

A way to do this without Solver: With your %CHNG and PnL data in A2:B7, in D2 use this formula to create a score if Criteria equals A2:
=SUM(IF($A$2:$A$7>A2,$B$2:$B$7,0))
(entered as an array formula: CTRL-SHIFT-ENTER). Copy/paste that formula into D3:D7 to get a score for the other %CHNG values. For this set of data, this gives a max score of 8 when %CHNG is -0.2.
A formula to get the max of all the scores is:
=MAX(D2:D7)
And a formula to get the %CHNG value associated with the max is:
=INDEX(A2:A7,MATCH(MAX(D2:D7),D2:D7,0))
Hope that helps.

If you want to do it with Solver, then use the "Evolutionary" Solver and not the default "Simplex LP" Solver.
And cell C2 should be
=IF(A2>=criteria,B2,"")
etc
If the Solver wants the variables bounded just limit "criteria" to between -100 and 100.
HTH

Here's a way to get the maximum over a range. This is called the Maximum Subarray Problem see here.
As before I assume that your %CHNG and PnL data in A2:B7. First you'll need to sort the data based on %CHNG. Column D will hold "maximum ending here" and column E will hold "maximum so far". We'll also have "First" in column F and "Last" in column G --- these are the first and last values of %CHNG that correspond to the maximum sum.
In D2 through G2 enter these formulas:
=B2
=B2
=A2
=A2
Now in D3 through G3 enter these formulas:
=IF(D2+B3>=B3,D2+B3,B3)
=MAX(D3,E2)
=IF(D2+B3>=B3,F2,A3)
=IF(E3=D3,A3,G2)
Copy/paste those formulas down to the end of your data. Cell E7 gives the maximum sum. Cells F7 and G7 give the values of %CHNG over which PnL is summed to get the maximum sum.
Hope this helps.

Related

Conditional Formula for excel

I am trying to do addition/subtraction from a total of one column, based on the value of a different column.
Here are the details:
Column H2:H61 has 3 possible values: Complete, Incomplete, Does Not exist.
Column I2:I61 are integers.
What I'm trying to accomplish is, for each Row in column H, evaluate if the value is "Complete". If it is, then, in a running total cell, convert the corresponding Row in I to a negative number and add it to the total. If it isn't, leave the number a positive number and add it to the total.
Example:
H2 = "Complete" I2 = 1.5
h3 = "Incomplete" I3 = 0.5
h4 = "Complete" I4 = 2.0
The total is 3
EDIT
Here is the full scope of it:
Excel Screenshot
So, the total values of I and L is currently 40.
What I'm trying to do is, for example, if H2 = "Complete", then I want to subtract I2 (which is 1.5), which would change the total value to 38.5.
H3 is "Does Not Exist" and != "Complete", so the total would still be 38.5.
H4 is "Complete", so the total would be 37.5
so on and so forth. Hope this helps clarify for everyone!
Try following formula in K2 cell then drag and down.
=SUMIF($H$2:$H2,"Complete",$I$2:$I2)-SUMIF($H$2:$H2,"Incomplete",$I$2:$I2)
I guess you want something like:
=40-SUMIF(H:H,"Completed",I:I)
assuming you do not have the 40 total in either Column H or I.
I figured it out.
What I did was in a separate cell I made the following formula:
=SUMIF(H2:H61,"Completed",I2:I61)
From there, I used that cell to create the following formula to get the result I wanted:
=P31 - (S30+S31)
The S30 and S31 cells are there because I was counting for each "completed" value in two separate columns.

Excel printing a specific number in a row subject to other columns

I have an excel file with 3 columns and 100 thousand rows. My goal is to print the number of column A, where the number in column C is the maximum and the number in column B is higher or equal to 0.9. Like this example:
-----+------+-----
A | B | C
-----+------+-----
1 | 0.9 | 130
2 | 0 | 200
3 | 0.95 | 90
In this example for example it should print '1' since column 1 and 3 are higher than 0.9 but 1 is higher than 3 in column C. Anyway to do this in excel?
Assuming your data in column C is positive (or at least, that the maximum value is positive), you can use this array formula:
= INDEX(A:A,MATCH(LARGE(((B:B>=0.9)+0)*(C:C),1),((B:B>=0.9)+0)*(C:C),0))
Note this is an array formula, so you must press Ctrl+Shift+Enter after typing in the formula.
This gets pretty ugly since there is no =MAXIFS() formula. Instead, an array formula will do the trick:
=SUMIFS(A1:A3, C1:C3,MAX(IF(B1:B3>=0.9, C1:C3, 0)), B1:B3,">=.9")
Hit Ctrl+Shift+Enter when entering that so Excel will interpret as an array formula.
The Sumifs() here isn't summing more than one value so no worries there. We are grabbing the value from A1:A3 where C1:C3 is equal to the MAX() of that column where B1:B3 is greater than or equal to .9. Which solves that max() issue. And then we are then only allowing selection A1:A3 where B1:B3 is greater than or equal to .9.
It's not pretty, and it requires us to check that >=.9 condition twice, but it does the job.
Try using this array formula =SUMPRODUCT(INDEX(A2:A20,MATCH(MAX(--($B$2:$B$20>=0.9)*C2:C20),--($B$2:$B$20>=0.9)*C2:C20,0),1)) in cell D2. Confirm it with CTRL+SHIFT+ENTER.
This is the array formula (means you have to click Ctrl + Shift + Enter altogether) what I came up with:
=INDEX(A:C,MATCH(MAX(--(B:B>=0.9)*(C:C)),(B:B>=0.9)*(C:C),0),1)
Please note that I used the whole column but I will suggest to only use the ranges that are needed for faster speed.

COUNTIF range does not equal range

The data I'm looking to get is something like this:
+----------+-------+
| Location | Count |
+----------+-------+
| Jungle | 2 |
| Ocean | 4 |
| Other | 2 |
+----------+-------+
The formula for count should look in the data range and if a cell's data does not equal any of the cell data in the rest of the location column (Jungle, Ocean) it should add that to the count. It should exclude blanks.
This is the formula I tried:
=COUNTIF(A1:H6,"<>"&E11:E12)
Here is my example sheet: https://docs.google.com/spreadsheets/d/1YbqEwa3olEXMcmU-UORfe7jwyK_-AXDomp1t5iTVUaU/edit?usp=sharing
Where am I going wrong? There are 7 other instances of colours not Green or Blue therefore I would expect this result to be output. I haven't put anything in about ignoring spaces so I would expect the result to count blanks too, but I get 0.
Don't use & - this would join E11 and E12 together so you would be looking for greenblue. You would need a COUNTIFS to get the three conditions
=COUNTIFS(A1:H6,"<>"&E11,A1:H6,"<>"&E12,A1:H6,"<>")
Here is a more general way of doing it
=COUNTA(A1:H6)-SUMPRODUCT(COUNTIF(A1:H6,E11:E12))
See this useful reference
The COUNTIFS formula is your friend here. This formula should work for that cell:
=COUNTIFS(A1:H6,"<>"&E11,A1:H6,"<>"&E12,A1:H6,"<>")
The problem with a 'does not equal OR does not equal' is that when it is not equal to one, it could be equal to the other and vice-versa. Additionally, you range to count from is 2 dimensional so you cannot use rows of criteria. Use SUMPRODUCT and individual criteria multiplied against each other.
=SUMPRODUCT((A1:H6<>"")*(A1:H6<>E11)*(A1:H6<>E12))
Any of those three conditions that is not true is considered zero. Anything multiplied by zero is zero. 1 x 1 x 1 is 1.
For large numbers of exclusions, you will want to use an array formula (with CSE) to count them and then subtract that total from a COUNTA total of the entire range.
=COUNTA(A1:H6)-SUM(COUNTIF(A1:H6,E11:E15))

Keep excel offset within worksheet

I have a table like this:
Length 4
year 1 2 3 4 5
A 100 400 300 200 400
B
And in column B I want a sum of A from the past [length] years. For this I figured I needed an OFFSET, so my function is (for year 2):
=SUM.IF(OFFSET(B3;0;0;1;-B1);">0")
The if statement is used so it doesn't give an error when it reaches the edge of the table, but for years 2 and 3 the OFFSET range is outside of the worksheet so it doesn't work. How can I specify a condition that it just doesn't sum anything that isn't on the worksheet?
In A2:
=SUM(INDEX(1:1,COLUMNS($A:A)):INDEX(1:1,MAX(1,COLUMNS($A:A)-3)))
Copy to the right as required.
Regards
Ok, it was hard to decypher the question:
When you ask column B, I guess you mean row 4, right?
You don't need SUMIF, because SUM doesn't count empty cells or cells
with non-numeric value.
The reference to the length value should be absolute, so it doesn't
change as you copy the formula:
$B$1
OFFSET's Width value cannot be negative, rather have the Cols value =
-[Length]:
OFFSET(C3;0; -$B$1...
(Now you are referencing 4 columns left to C3)
Make sure it is not out of the worksheet by not letting more than the [column number of the given cell minus 1] be referenced left from the cell:
OFFSET(C3;0; -MIN(COLUMN(C3)-1;$B$1)...
That is the starting point of your range to sum; you should sum it
up to recent year's value. So the correct formula in C4 is:
=SUM(OFFSET(C3;0;-MIN(COLUMN(C3)-1;$B$1)):C3)

Excel matching multiple cells for duplicates

I need to populate a cell where the result is either valid or an error based on the following criteria. I'm not sure if using Match, Lookup formulas will work for this problem.
Given
A B C
+-----------+-----------+----------
1 | IntRef | Value | Result
2 |-----------|-----------+----------
3 | r01 | Value 123 | Success (because B4 matches B3)
4 | r01 | Value 123 | Success (because B3 matches B4)
5 | r02 | Value ABC | Failed (because B6 differs from B5)
6 | r02 | Value XYZ | Failed (because B5 differs from B6)
Success Criteria
Scan each IntRef (A) column for all duplicate keys. Where they match
on a row check the Value column (B). Where all matching cells have
the same value set their result cell (C) to Success.
Failed Criteria
Scan each IntRef (A) column for all duplicate keys. Where they match
on a row check the Value column (B). Where all matching cells have a
different value set their result cell (C) to Failed.
I am sure there is a formula that can be entered into each cell of column C which will do a lookup for each IntRef cross referencing the contents of column B where the match occurs. This is going beyond Excel formula knowledge.
Is it possible to create and help formulate the calculation of the success/failed criteria (Column C)?
This appears to do the trick...
{=IF(COUNT(IF($B$3:$B$6=B3,IF($C$3:$C$6=C3,1)))=COUNTIF($B$3:$B$6,B3),"Success","Failed")}
Note that that's an array lookup formula (meaning you need to hit Ctrl+Shift+Enter when entering it).
This formula basically counts the number of times the A and B column values appear together and compares this to the number of times the A column value appears. If the two counts match, you have success.
Try this formula:
=IF(SUMPRODUCT(IF(A2=A$2:A$9,1,0),IF(B2=B$2:B$9,1,0))>1,"Success","Fail")
Assuming you have your data like this:
Formula is entered as Array Formula in C2 by pressing Ctrl+Shift+Enter.
Then just copy on the remaining cells.
I just added and changed the position of some data for testing.
Hope this works for you. Change the Range to suit your data size.

Resources