Excel: Counting the Dependents of a cell - excel

How want to implement a function in Excel that can count the dependents for a given cell.
Example, suppose the following data:
| | A | B | C |
| 1 | |=A4|=A4|
| 2 | |=A4|=A4|
| 3 | | | |
| 4 |foo|=DEPENDENTCOUNT(A4)
In, cell B4, I want to show the number of time the cell A4 was referenced. With this table, it should 4, since =A4 was used 4 times.
I have tried the following function, but it always returns 1:
Function DEPENDENTCOUNT(cell) As Integer
DEPENDENTCOUNT = cell.Dependents.Count
End Function
Any idea?

First there is a spelling error in the UDF():
Function DEPENDENTCOUNT(cell As Range) As Integer
DEPENDENTCOUNT = cell.Dependents.Count
End Function
Secondly, that Property will work within a Sub, but not within a UDF():
Sub qwert()
MsgBox Range("A1").Dependents.Count
End Sub

Related

Excel: Partial matching Range 1 against each cell string individually in Range 2, then returning value in column

If you can help me I love you.
Let's say I have a bunch of URLs:
| Range 1 |
|------------------------|
| www.orange.com |
| www.orange.example.com |
| www.example.red.com |
| www.example.com/blue |
And I also have a table like this:
| Range 2 | Range 3 |
|---------|---------|
| orange | 1 |
| red | 2 |
| blue | 3 |
| green | 4 |
| pink | 5 |
How could I write a formula to pull down alongside the URL list so that it looks like:
| Range 1 | Results |
|------------------------|---------|
| www.orange.com | 1 |
| www.orange.example.com | 1 |
| www.example.red.com | 2 |
| www.example.com/blue | 3 |
Essentially doing partial matches every time and then returning a result to the right.
Driving me nuts, you're my only hope!
Google Sheets.
=ArrayFormula(LOOKUP(1,0/COUNTIF(A1,"*"&C$1:C$5&"*"),D$1:D$5))
MSOFFICE
=LOOKUP(1,0/FIND(C$1:C$5,A1),D$1:D$5)
This will only work if the string does not contain multiple partial strings in your list, but you can just INDEX the lookup array using the row number of a partial match.
For example:
=IFERROR(
INDEX($B$1:$B$5, CONCAT(IFERROR(
FIND($A$1:$A$5,D5)+ROW($A$1:$A$5)-FIND($A$1:$A$5,D5),
""))),"")
Here we first generate a vector testing the location of all values in the lookup array against the string using FIND. All but one element in this vector are #VALUE!. The remaining element is the location of the first character in the string being tested that is the same as the value in the lookup array.
Next we convert this value to the row number in the lookup array by adding another equal sized vector of sequential row numbers, and then subtracting the original quantity. Note that arithmetic on a #VALUE! return will still yield #VALUE!. Also note that the vector of row numbers as generated in the answer above (the ...ROW($A$1:$A$5)) must begin in the first row; if your lookup array starts in row 3 for example, you would need to do something like ...ROW($A$3:$A$7)-2 to get the result you desire.
Finally, we replace any #VALUE! return with an empty string and concatenate the entire vector. The result is the row number of the lookup array which contains a partial string in the full string. This row number is used in an INDEX to achieve the desired results.

Sum All VLOOKUP Matches

I have a sheet where I am recording what I eat:
Another where I keep an index of values to lookup
I tried
=SUM(VLOOKUP('Sheet1'!A2:A11,'Sheet2'!A2:E11,2,FALSE))
but that only returned the first match, so then I tried
=SUMPRODUCT(SUMIF('Sheet1'!A2:A11,'Sheet2'!A2:A11,'Sheet2'!B2:B11))
but that isn't working either.
does anyone have a solution, where I can also multiply the value of the return match by the # of servings in the first sheet?
Thanks!
If you want a single output of calories through SUMPRODUCT then you can use
=SUMPRODUCT(B2:B11*IFERROR(VLOOKUP(A2:A11,Sheet2!A2:B11,2,0),0))
If you are sure that all entries on Sheet 1 can be located on Sheet 2 then you can drop IFERROR portion like
=SUMPRODUCT(B2:B11*VLOOKUP(A2:A11,Sheet2!A2:B11,2,0)).
Beware that if a value is not found in Sheet 2 then formula will produce wrong result as IFERROR will multiply the serving quantity with 0.
I combine 2 tables into one sheet, Table 1 housed in Column A & B and Table 2 housed in Column D & E
In G2, "Total Serving Colories" enter formula :
=SUMPRODUCT(VLOOKUP(T(IF({1},A2:A12)),D2:E12,2,FALSE)*B2:B12)
It's not super-clear what you're trying to get at. But defining the "Calories Per Serving" in a range called "cals",
+---+---------+-----+--------------------------------+
| | A | B | C |
+---+---------+-----+--------------------------------+
| 1 | egg | 3 | =(VLOOKUP(A2,cals,2,FALSE))*B2 |
| 2 | oatmeal | 1.5 | =(VLOOKUP(A3,cals,2,FALSE))*B3 |
| 3 | shrimp | 2 | =(VLOOKUP(A4,cals,2,FALSE))*B4 |
+---+---------+-----+--------------------------------+
Results in:

Google Spreadsheet - Sum cell value if another cell contain one of N strings

As shown on this Google Spreadsheet I would like to SUM a list of currency values from a column only if in the row of the value I have one of the selected strings from a list.
| Tag | Value |
| : | : |
| Goo | 12$ | <= SUM value because I have Goo or Boo
| Dee | 3$ |
| Boo | 4$ | <= SUM value because I have Goo or Boo
| Yoo | 7$ |
| : | : |
| Result | 16$ |
I didn't found a way to do that, is that possible?
You can just sum two SUMIF() functions together.
=SUMIF(A:A,"Goo",B:B) + SUMIF(A:A,"Boo",B:B)
Assuming that A:A contains the words to match and B:B contains the values to sum.
To be more specific to your issue, you will actually have to modify your range so you are not getting any circular reference errors (since the cell is in the same column as your sum range)
So, if the cell that contains your formula is in row 25 (for example), then something like this should work:
=SUMIF(A1:A24,"Goo",B1:B24) + SUMIF(A1:A24,"Boo",B1:B24)
=ArrayFormula(SUMPRODUCT((A2:A5=({"Goo","Boo"}))*(B2:B5))) - SUMIF has a lot of inherent limitations, you would do well to read something about array formulas.

AutoIncrement ID Number (Works with Sorting)

Good Morning,
I'm constructing a table and I'd like to assign each entry a unique ID number that autoincrements as new values are entered into the table. Here's a sample table to give a visual:
+----+------------+-----------+
| ID | First Name | Last Name |
+----+------------+-----------+
| 1 | John | Smith |
| 2 | Steve | Wozniak |
| 3 | Steve | Jobs |
+----+------------+-----------+
In the above table, if I enter a 4th person, what I would like to happen is that the ID is automatically generated (in this case as 4). I've tried an in cell formula such as:
=Row([#ID])-1
This works until you sort the table, in which case the ID numbers get all scrambled up. I'd like to calculate what the row number should be, but then keep that value constant. Any ideas?
If you want a static increment key, you should use a code like this in your output worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then
Target.Offset(0, -1).Value = Target.Row - 1
End If
End Sub
If you just want a unique key that isn't actually incrementing based on previous one, you can use a formula like =SUM(CODE(MID(B2&C2,ROW(INDIRECT("1:"&LEN(B2&C2))),1))) in your A2 cell (closing with CSE) and pull down. However, if you extend a formula too much it will affect your sheet's performance.

MAX date value within a range with 2 conditions

To make it easy
+---+----+-------------+
| | A | B |
+---+----+-------------+
| 1 | xx | 12-05-2015 |
| 2 | xx | 15-05-2015 |
| 3 | yy | 13-05-2015 |
| 4 | yy | 16-05-2015 |
+---+----+-------------+
(today is 14-05-2015)
I need to get the MAX date value for each "A" value only if it is before today.
In case it's not, move to the 2nd biggest value. Case it does not find, empty cell.
What I've done so far:
=MAX($A$1:$A$4='xx';$B$1:$B$4<TODAY();$B$1:$B$4)
and confirm with SHIFT+CTRL+ENTER
The error I get is that it yields 13-05-2015 as max value for xx, which is obviously wrong (as if it does not take into account the $A$1:$A$4='xx'
You need to use nested if-functions. I.e. change your formula into:
{=MAX(IF($A$1:$A$4="xx", IF($B$1:$B$4<TODAY(), $B$1:$B$4)))}
And end it with Ctrl+Shift+Enter
A standard (non-array) formula alternative.
=MAX(INDEX((B:B)*(A:A="xx")*(B:B<TODAY()), , ))
      
This formula would benefits from having its cell ranges cut down from full columns to something closer to the usable data range.
If your dates are sorted ascending as shown in the example then you can use LOOKUP like this:
=LOOKUP(2,1/(A$1:A$100="xx")/(B$1:B$100<TODAY()),B$1:B$100)
Doesn't require "array entry"

Resources