Excel VBA sum(sumifs) within sumproduct only works with F9 - excel

Situation:
Row 1: Variable | Margin | Sales
Row 2: banana | 2 | 10
Row 3: apple | 5 | 20
Row 4: apple | 10 | 20
"Variable" sits in column A, row 1.
Sales = name range for variable column i.e. A2:A4
Fruit = name range for "apple" and "banana" i.e. {apple;banana}
I have the above table and what to find the weighted average Margin. The weighting is based on sales.
Formula used: =SUMPRODUCT(B2:B4,C2:C4/(SUM(SUMIFS(C2:C4,sales,fruit))))
Problem:
When I use this formula I get #DIV/0!
If i step into the formula and press F9 I get 6.40 which is the correct answer.
Could someone please tell me what I am doing wrong? How can I get 6.40 without stepping into the formula with F9.
Note:
I would like to avoid using this formula: =SUMPRODUCT(B2:B4,C2:C4/(SUM(SUMIFS(C2:C4,sales,{"apple","banana"}))))
The range of fruit included in the calculation is dynamic. Sometimes I'd like to include only banana, sometimes only apple and I could make it dynamic by changing the cells within the fruit named range.

Related

I have a list of keywords and want to count the number of match keywords in each cells text from EXCEL File

I have 2 sheet in a excel file. one is a Dictionary sheet and 2nd is a sheet containing a column of text. I want to match the dictionary keywords columns one by one and then the number of match keywords counts in each cell of the text column.
I have tried these formulas:
=(LEN(B2)-LEN(SUBSTITUTE(B2,Sheet1!A:A,"")))/LEN(Sheet1!A:A)
in this B2 is the first (start) cell of the text column and Sheet1!A:A is the dictionary column of other sheet.
but by this i get zero as a result
=(LEN(B2)-LEN(SUBSTITUTE(B2,Sheet1!A:A,"")))/LEN(Sheet1!A:A)
The result will be like this:
Text number_of_keyword_match | number_of_keyword_match using DIC col 2 | ........
using DIC col 1
1 any Text or sentence/sentences e.g match "3"
2 7
3 0
4 15
5 .................................................
7 .....................................................
.......................................................
..................................continue up to 2815 rows....
Assuming your text input looks like this:
| A |
-+----------------------+
1|apple apple beat beat |
2|apple beat beat carrot|
3|carrot apple apple |
and your dictionary looks like this:
| A | B |
-+-------+-------+
1|apple |beat |
2|beat |carrot |
3| | |
This formula will give you the count per word per cell of text
=(LEN(text!A1)-LEN(SUBSTITUTE(text!A1,dictionary!A1,"")))/LEN(dictionary!A1)
(In this example 2)
If I understand correctly, your expected output would be extra columns in the text sheet where each cell contains the sum of counts of each word in the corresponding column in dictionary, right? For example:
| A | B | C |
-+----------------------+---+---+
1|apple apple beat beat | 4 | 2 |
2|apple beat beat carrot| 3 | 3 |
3|carrot apple apple | 2 | 1 |
You can use array formulas to do this, starting with this one in cell B1:
=SUM(IFERROR((LEN(text!$A1)-LEN(SUBSTITUTE(text!$A1;dictionary!A:A;"")))/LEN(dictionary!A:A);0))
But instead of pressing Enter after pasting it in, press Ctrl+Shift+Enter to run it as an array formula. Then drag this formula down and to the right to get all the counts you want.
I would consider countif(), as an example:
=COUNTIF(Sheet2!A1:A10,Sheet1!A1)
which assumes your list starts in cell A1 on sheet1 and your text in cell a1 on sheet2.
To drag the formula as I showed it, without the data range moving you need :
=COUNTIF(Sheet2!A$1:A$10,Sheet1!A1)
if you put $ before the letters, then the columns don't move.

Excel INDEX / MATCH / MATCH across cell range

I am trying to get the formula right to lookup a value and return the cell underneath that cell, but I can't seem to get the code right and a MATCH/MATCH does not seem to do the trick (at least to the best of my knowledge).
Let's suppose my data set looks like this:
Col1 Col2 Col3
---------------------
Row 1 | P(A1) P(A2) P(A3)
Row 2 | 10.5% 11.5% 12.5%
Row 3 |
Row 4 | P(B1) P(B2) P(B3)
Row 5 | 50.5% 60.6% 70.5%
Row 6 |
Row 7 | P(C1) P(C2) P(C3)
Row 8 | 25.2% 34.6% 88.5%
Now I have a reference cell, say A1, which can have the value P(C2) and I want to return the corresponding value underneath P(C2), i.e. 34.6% in the cell A2.
I hope I am not being too cryptic and thanks very much in advance for any assistance.
Best
Max
Try the following...
F2, confirmed with CONTROL+SHIFT+ENTER...
=INDEX(A2:C9,SMALL(IF(A2:C9=E2,ROW(A2:C9)-ROW(A2)+1),1)+1,MATCH(E2,INDEX(A2:C9,SMALL(IF(A2:C9=E2,ROW(A2:C9)-ROW(A2)+1),1),0),0))

Excel solver, variable cell to change adjusts data set

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.

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.

Excel Function Help - Compare 2 Cells in Worksheet to 2 Cells in another worksheet, if they match, return value from a different column

I'm wondering if someone would be able to offer some advice on the best way to do this please:
Data in Worksheet # 1
Col A | Col B | Col C
Part-1 | 8 | 2
Part-2 | 7 | 7
Part-7 | 9 | 4
Part-8 | 2 | 6
Data in Worksheet # 2
Col A | Col B | Col C
Part-1 | 8 | *Return value* (If Part-1 is found and 8 matches, return 2)
Part-2 | 7 | *Return value*
Part-3 | 8 | *Return value*
In Worksheet#2 in Cell C2 - I would like to check if the Part-1 in A1 is found in Col A in Worksheet#1. If it is, then I would also like to make sure the Number is B2 in Worksheet#2 matches the Qty in Col B next to the same part#, if both the Part# and Qty match, then i would like to copy across the value from the corresponding cell in Col C in Worksheet#1, to Col C in Worksheet#2. If it does not match, I would like it to return a 0.
Here is the a variation on Reinier's second approach in a form that will work in any version of Excel. You can use references to the specific data ranges, as I have done here, or to entire columns.
=SUM((A2=Sheet1!$A$2:$A$5)*(Sheet2!B2=Sheet1!$B$2:$B$5)*Sheet1!$C$2:$C$5)
This is an array formula, so it needs to be entered with the Control-Shift-Enter combination. It performs the same operation as the SUMPRODUCT. (There are several other ways to do this, such as using MATCH with INDEX or OFFSET.)
Essentially, what you are doing is a lookup based on values in two columns. Looking at it from that angle, you can use a the SUMPRODUCT function, which is supported in any version of Excel. Enter the following in Sheet2, cell C2:
=SUMPRODUCT((Sheet1!$A:$A=$A2)*(Sheet1!$B:$B=$B2)*Sheet1!$C:$C)
Select and drag down the right-bottom corner of C2 to complete column C.
This only works by virtue of the fact that the values in Sheet1, column C, are numbers. It breaks if value pairs in column A and B of Sheet1 occur multiple times, but you did not address that situation in your question in the first place.
For versions 2007 and up, you can use the more convenient function SUMIFS with basically the same approach:
=SUMIFS(Sheet1!$C:$C,Sheet1!$A:$A,$A1,Sheet1!$B:$B,$B1)
Alternatively, you can use a combination of IF and VLOOKUP functions. A formula that will work for Excel 2007 or newer is the following:
=IFERROR(IF(VLOOKUP($A1,Sheet1!$A:$C,2,FALSE)=$B1,VLOOKUP($A1,Sheet1!$A:$C,3,FALSE),0),0)
Older versions of Excel do not support IFERROR, but you can still use a similar approach as explained here.
I have uploaded an example workbook here, which includes an alternative method in column D of Sheet2 as well.

Resources