Excel: sum this row of named ranged columns - excel

I have a dynamic number of columns that i add via VBA. These columns are a named range. How can I sum the current row of all the columns in that named range?
Example:
____| A | B | C | D | E |
1 | | 123 | 100 | aaa | 223 |
2 | | 111 | 101 | eee | 212 |
3 | | 112 | 102 | xxx | 214 |
4 | | 197 | 103 | yyy | 300 |
5 | | 176 | 104 | zzz | 280 |
let's say columns B and C are a named range called ser_ua. I don't know if there will be no columns at all in this named range or if there will be 50 columns. How can i sum the current row "slice" in (in this case) E?

You can 'slice' off a column with the INDEX function, using the MATCH function to find the last number in the first row.
=sum(index(ser_ua, 0, match(1e99, 1:1)))
Any other column could be 'sliced' off using the column_num parameter of INDEX either with a hard-coded number or some formula returning a number.
        
If ser_ua is a named range starting at B2 then you cannot ask for hte column number of the last number in row 1. You would have to ask for the last number on the first row of ser_ua.
=sum(index(ser_ua, 0, match(1E+99, index(ser_ua, 1, 0))))
        

Since you are already using VBA,:
Public Function SumAcross(N As Long) As Variant
Dim wf As WorksheetFunction, RangeToAdd As Range
Set wf = Application.WorksheetFunction
Set RangeToAdd = Intersect(Rows(N), Range("ser_ua"))
SumAcross = wf.Sum(RangeToAdd)
End Function
EDIT#1:
To improve the volatility of the UDF, we can pass it the range being examined:
Public Function SumAcross(r As Range, N As Long) As Variant
Dim wf As WorksheetFunction, RangeToAdd As Range
Set wf = Application.WorksheetFunction
Set RangeToAdd = Intersect(Rows(N), Range("ser_ua"))
SumAcross = wf.Sum(RangeToAdd)
End Function
and in a typical worksheet cell:
=SumAcross(ser_ua,ROW())

Related

I have data stored in excel where I need to sort that data

In excel, I have data divided into
Year Code Class Count
2001 RAI01 LNS 9
2001 RAI01 APRP 4
2001 RAI01 3
2002 RAI01 BPR 3
2002 RAI01 BRK 3
2003 RAI01 URE 3
2003 CFCOLLTXFT APRP 2
2003 CFCOLLTXFT BPR 2
2004 CFCOLLTXFT GRL 2
2004 CFCOLLTXFT HDS 2
2005 RAI HDS 2
where I need to find the top 3 products for that particular customer for that particular year.
The real trick here is to rank each row based on a group.
Your rank is determined by your Count column (Column D).
Your group is determined by your Year and Code (I think) columns (Column A and B respectively).
You can use this gnarly sumproduct() formula to get a rank (Starting at 1) based on the Count for each Group.
So to get a ranking for each Year and Code from 1 to whatever, in a new column next to this data:
=SUMPRODUCT(($A$2:$A$50=A2)*(B2=$B$2:$B$50)*(D2<$D$2:$D$50))+1
And copy that down. Now you can AutoFilter on this to show all rows that have a rank less than 4. You can sort this on Customer, then Year and you should have a nice list of top 3 within each year/code.
Explanation of sumproduct.
Sumproduct goes row by row and applies the math that is defined for each row. When it is done it sums the results.
As an example, take the following worksheet:
+---+---+---+
| | A | B |
+---+---+---+
| 1 | 1 | 1 |
| 2 | 1 | 4 |
| 3 | 2 | 2 |
| 4 | 4 | 1 |
| 5 | 1 | 2 |
+---+---+---+
`=SUMPRODUCT((A1:A5)*(B1:B5))`
This sumproduct will take A1*B1, A2*B2, A3*B3, A4*B4, A5*B5 and then add those five results up to give you a number. That is 1 + 4 + 4 + 4 + 1 = 15
It will also work on conditional/boolean statements returning, for each row/condition a 1 or a 0 (for True and False, which is a "Boolean" value).
As an example, take the following worksheet that holds the type of publication in a library and a count:
+---+----------+---+
| | A | B |
+---+----------+---+
| 1 | Book | 1 |
| 2 | Magazine | 4 |
| 3 | Book | 2 |
| 4 | Comic | 1 |
| 5 | Pamphlet | 2 |
+---+----------+---+
=SUMPRODUCT((A1:A5="Book")*(B1:B5))
This will test to see if A1 is "Book" and return a 1 or 0 then multiple that result by whatever is B1. Then continue for each row in the range up to row 5. The result will 1+0+2+0+0 = 3. There are 3 books in the library (it's not a very big library).
For this answer's sumproduct:
So ($A$2:$A$50=A2) says to return a 1 if A2=A2 or a 0 if A2<>A2. It does that for A2 through A50 comparing it to A2, returning a 1 or a 0.
(B2=$B$2:$B$50) will test each cell B2 through B50 to see if it is equal to B2 and return a 1 or 0 for each test.
The same is true for (D2<$D$2:$D$50) but it's testing to see if the count is less than the current cells count.
So... essentially this is saying "For all the rows 1 through 50, test to find all the other rows that have the same value in Column A and B AND have a count less than this rows count. Count all of those rows up that meet that criteria, and add 1 to it. This is the rank of this row within its group."
Copying this formula has it redetermine that rank for each row allowing you to rank and filter.

Excel macro to find a date in a range and then count for next value change in an adjacent column

I am attempting to write a macro to find February 2nd of each year in column A and then count the number of rows (days) until the value in column B changes. This count could be put in a new column, column C, but on the same row as the February 2nd that it correlates to, in this case row 3.
Using the table below the output to C3 would be 5. I am not counting the day of February 2nd but I am counting the day the change occurs. This is for 100+ years that I will need to loop through.
id | A | B | C
----------------------------
1 | 1946/01/31 | 0 |
2 | 1946/02/01 | 0 |
3 | 1946/02/02 | 0 |
4 | 1946/02/03 | 0 |
5 | 1946/02/04 | 0 |
6 | 1946/02/05 | 0 |
7 | 1946/02/06 | 0 |
8 | 1946/02/07 | 2 |
9 | 1946/02/08 | 0 |
The real challenge is to do it with a formula. Well, 2 formulas.
The first formula in cell E2 finds the date 2nd Feb by looking for "02/02" at the end of the text in column B and if it is found it places the contents of C2 in that cell. if it's not found it compares C1 with D1, the 2 cells above to see if they are the same because a match was previously found and if so it takes the contents of the cell above. This results in the zeros you can see in column E between 2nd Feb and the point where column C changes.
Formula for E2 and then autofill down to the end of your data
=IF(AND(MONTH(B2)=2,DAY(B2)=2),C1,IF(AND(E1<>"",E1=C1),E1,""))
Now all we need to do is count the cells in column D by looking for the first non blank cell in column D AND(E1="",E2<>"") and then count all the cells that match that cell. I'm not sure what gap you're expecting to find but you can change the 200 to ensure that you count everything. The last part is to take away 1 so that the 2nd feb row is not being counted.
Formula for D2 and then autofill down to the end of your data
=if(AND(E1="",E2<>""),countif(E2:E200,E2)-1,"")

Consistent formula in Cell

In Excel, I want to make consistant formula in a cell.
| A | B |C
--|-------|----|----------------------------------------
1 | Date1 | 12 |=B1
2 | Date1 | 45 |=B2+B1
3 | Date1 | 20 |=SUMIF(A1:A100, A3, B1:B100)
4 | Date2 | 10 |=B4
5 | Date2 | 5 |=B4+B5
6 | Date2 | 28 |=B4+B5+B6
7 | Date2 | 88 |=SUMIF(A1:A100, A7, B1:B100)
I used different formulas in cell C for each row. How to make Consistent Formula in Cell C?
Is it possible to select particular range in SUMIF function? e.g. Instead of SUMIF(A1:A100, A7, B1:B100), I want SUMIF(A1:A100, A7, B4:B7)
This single formula will do it
=SUMIFS($B$1:B1,$A$1:A1,A1)
Enter in C1, copy down as far as required.
Note the $'s. These anchor the Criteria Range and Sum Range at row 1. When the formula is copied down these ranges will expand down to the row the formula is in.

Excel: select column in range by column index - no vba

I have cell A1, and then below 4 columns. Cell A1 right now contains number 2.
2
| A | B | C | SELECTED |
| 12 | 44 | 88 | 44 |
| 43 | 55 | 99 | 55 |
| 54 | 66 | 11 | 66 |
Which formula should I put into 'Selected' column to display there values of the second column? I would like the A1 cell to control which values are displayed there. Basically, I'm looking for a similar functionality to a VLOOKUP, except there is no lookup and it just takes range a4:c4 and selectes column number A1.
You can use INDEX() for that. Let's say that 12, 44 and 88 are in columns A, B and C respectively, and on row 4.
In the first cell under SELECTED, you can put this:
=INDEX(A4:C4, 0, A$1)
And then you can drag this down to get the remaining rows.
INDEX takes the whole row (hence the 0) and the column from the value in A$1 from the range A4:C4.
In D2 enter:
=INDEX(A:C,ROW(),$A$1)
and copy down

Get a Sum of ONLY duplicate values

If there a way to use a sumif combined with a countif?
for example if I have
TransactionNo|NumberToSum
1 |
1 | 9
2 | 6
3 | 3
4 |
4 | 4
5 | 6
6 | 4
7 |
7 | 9
8 | 7
9 | 4
10 |
10 | 1
11 | 3
12 | 6
My result would be: 23
because the values 1,4,7,10 are duplicated, so I need to sum the NumberToSum of those 9,4,9,1 in this case.
Ideally I would like somethings along the lines of =SumIf(A:A,Count>1,B:B)
Where B is summed if the count of A inside of A:A is > 1.
but =SUMIF(A:A,COUNT(A:A)>1,B:B) does not work.
NOTE: the Data is NOT sorted as it appears in my example.
If I wanted to do it in VBA one quick way could be:
Sub sumdups()
Dim rng As Range
Dim rng2 As Range
Dim Sum As Long
Set rng = [A2:A17]
For Each rng2 In rng
If WorksheetFunction.CountIf(Range("A1:A17"), rng2.Value) > 1 Then
Sum = Sum + rng2.Offset(0, 1).Value
End If
Next rng2
End Sub
Another option I have tried is to add a column with the formula
=IF(COUNTIF(A:A,A2)>1,B2,"")
Then sum the helper column, but I am hoping there is a simple formula that can do the same all in one cell.
I am really just looking for a faster formula that can be entered in a single cell, without the need for any additional calculations.
This formula works for me:
=SUMPRODUCT(1*(COUNTIF(A1:A100,A1:A100)>1),(B1:B100))
or for entire column (a little bit slower):
=SUMPRODUCT(1*(COUNTIF(A:A,A:A)>1),(B:B))

Resources