Index/Match and sum a range based on match - vba

Good morning. Have tried asking this several times with no success.
I have written VBA code with several index/matches.
pasterOH = "=IFERROR(IF(RC[-3]=""Subtotal"","""",IF(RC[-4]="""",INDEX(" & stapler & OH1 & "'!" & "C" & (Month(Worksheets("SEL Onsite OH").Cells(6, 6).Value) + 6) & ",MATCH(RC[-3], " & stapler & OH1 & "'!C3,FALSE),1),INDEX(" & stapler & OH1 & "'!" & "C" & (Month(Worksheets("SEL Onsite OH").Cells(6, 6).Value) + 14) & ",MATCH(RC[-4], " & stapler & OH1 & "'!C2,FALSE),1))),""0"")"
The code works fine to look up a cell in the column that I have specified and return a dollar value if it is a match.
Here is my issue: I need to be able to sum all of the dollar values to the left of the match. For example, in the picture I posted a simple picture of a table with index/match. In the formula the target is column E:E, whereby "tom' returns "5". What I am trying to do is if excel can find and match "tom" in column 5, it would sum B2,C3,D4,E5. I could write this if I knew what column "tom" would be in every time. I need to have a VBA solution that goes along with the pasterOH from above that will allow me to sum the values of column 7 (January) picked by my (Month(Worksheets("SEL Onsite OH").Cells(6, 6).Value) + 6)through whatever column the month happens to be.
For example, In May, the column I am looking up and returning a match to is column 11. I would like the code to start with the matching entry (our "tom" in this case), and sum the results of Jan- May (columns 7-11) and return that as the answer instead of tom's result only in May.
Hope that is clear. I posted a couple of times and found this answer, but don't know if it will apply or how to edit my VBA formula.
Thanks for any assistance
Extending a conditional index-match to sum across a range
Sample table pic

The formula you want is
=SUM(INDEX(B:F,MATCH(C11,A:A,0),0))

Related

Summing in VBA with a variable column and range

I know this question gets asked a lot but I haven't been able to find the answer I'm looking for. I'm trying to write a code that will be able to find a key term and then sum that column for a certain amount of rows.
I've tried simply replacing "G" in this code with my variable for the correct column (col) and I've made sure that my column variable is matching to the correct column.
Cells(subRow, col).Formula = "=SUM(G" & row & ":G" & subRow & ")"
The above, for example, works; but I would like it to look like this:
Cells(subRow, col).Formula = "=SUM(col" & row & ":col" & subRow & ")"
I've tried moving the col variable around in and outside of the quotes, and I can't seem to find a way to do it.
Thanks in advance.
Based on the case you have described, you would have to use R1C1 Reference Style:
Cells(subRow, col).FormulaR1C1 = _
"=SUM(R" & row & "C" & col & ":R" & subRow & "C" & col")"
There is also a Cells(row, column).Resize(number of rows, number of columns) option :
Cells(subRow, col).Formula = "=SUM(" & Cells(row, col).Resize(subRow - row, 1).Address(0, 0) & ")"

Writing a formula as Syntax in VBA

I want to run a rank formula across a range of cells. I am scratching my head as to why it's not working. Lastrow is just the formula that counts the number of rows.
Range("B1:B" & Lastrow).Formula = "=RANK(A1,Offset(" & Chr$(36) & "A1" & Chr$(36) & "," & Lastrow & ",0))"
I feel like it's something wrong with Chr$(36), but when I try Chr(36) it doesn't work either. (removing these chr(36)'s and just having Offset(A1... etc) works fine).
Thanks in Advance!
It's hard to see what you want to do. The line of VBA code below will, at least, work.
Range("B1:B" & LastRow).Formula = "=RANK(ROW(),$A1:$A$" & LastRow & ",0)"
Your issue seems to be with the RANK() function. It has 3 arguments, (1) the rank, (2) the range in which to find the rank and (3) Ascending/Descending. In your formula the first argument is missing and your "Offset(" & Chr$(36) & "A1" & Chr$(36) & "," & LastRow" doesn't describe a range, read as Offset($A1$,300 with closing bracket missing.
My above formula suggests the Row number as rank, meaning 1 for Row 1, 2 for Row 2 etc. but descending, as indicated by the 3rd argument (taken from your formula) and, as second argument a range in column A between A1 and the LastRow. It probably isn't what you wanted but I hope you will be able to tweak it.

Vlookup VBA taking huge time and returning wrong values

I am using a vlookup formula in VBA
.Range("BO2:BO" & NewLastRow).Formula = "=VLOOKUP(BN2,'" & OldSheet.Name & "'!$BL$2:$BM$" & OldLastRow & ",2,0)"
As this a case of Exact match, it is taking huge time. But when i change it to approx match ,it is returning wrong values. Basically i want to vlookup dates from other worksheet.

Loop Through Columns to Insert SUM Formula

I have a program where I need to find the sum of columns 7 through 30 (G through AD). What I am trying to do is loop through and insert the formula (=SUM(Columns(i)2:Columns(i)1000)) but obviously Columns(i) is not the letter but the number. This is not complying with the format required by SUM so I'm wondering what I can do instead.
I have a program where old sheets will be deleted and new ones added holding configuration data for a product. This means formulas cannot be in the sheet inself or any other sheet referring to it. Prices will be held in columns G through P and U through AD which I need to find the total for and place it in row 1 above the corresponding data. When I try:
For i = 7 To 30
wsNewSheet.Cells(1, i).Value = "=SUM(" & Columns(i).Select & "2:" & Columns(i).Select & "1000)"
Next i
Columns(i) is returning as "True" for some reason. I have also tried to place the totals in a different sheet as the new sheets (wsNewSheet) were being created.
For i = 2 To 30
For f = 7 To 30
wsTotals.Cells(1, i).Value = "=SUM(" & wsNewSheet & "!" & Columns(f).Select & "2:" & Columns(f).Select & "1000)"
Next f
Next i
However, this did not work either. This statement returned "Run-time error '438.' Object doesn't support this property or method." I attemped to do research on this error, but could not fix my situation. Thanks for any help.
You don't need loop here, use single line instead:
Range("G1:AD1").Formula = "=SUM(G2:G1000)"
Excel automatically adjust formulas:
in G1 you'd have =SUM(G2:G1000)
in H1 you'd have =SUM(H2:H1000)
....
in AD1 you'd have =SUM(AD2:AD1000)

Using .formula to sum values in dynamic ranges

I created a macro that changes the layout of a raw excel file and displays some usefull information to the user. But I am stuck now.
I want to display the sum of some cells in a column, but the cell where the sum is and the column itself is set dynamically.
My layout looks like this:
Year1 Year2 Year3
Sum1 100
Sum2 50 48
Sum3 72 81
Sum4 26
------------------------------------------
Total
I want to display the sum of (sum1, sum2, sum3, sum4) in the Total row under each Year.
The thing is my years are set dynamically depending on the value of a parameter (i.e. I can have 2, 3 or 5 years).
I know I can do it using Range.WorksheetFunction.Sum but the problem is my values are subject to some changes after the macro has been used and if I use this function the values won't change afterward.
That's why I want to use the function Range.Formula to display these values as I can enter the sum like I did for the values already in the sheet.
But unlike these values (that are the sum of values in an unique column easy to select), I am not able to select the colum dynamically.
Here is what I tried to give you an idea of what I want to do if it is unclear to you:
For i=0 to Duration-1
Sheet2.Range("D" & RNumber + 7 + Duration).Offset(0,1+2*i).Formula="=SUM(Sheet2!" & Range("D" & RNumber + 6).Offset(0,1+2*i) & ":Sheet2!" & Range("D" & RNumber + 6 + Duration).Offset(0,1+2*i) & ")"
Next i
But it doesn't work as the sum doesn't recognize the range. I know I should have something like
"=SUM(Sheet2!E" & Firstrow & ":Sheet2!E" & Lastrow & ")"
but then i won't be able to select my colum dynamically.
It looks like your problem stems from the fact that you can't do things like "Sheet2!" & D+3+1 for columns the same way you can do "Sheet2!D" & 5+6+2 for rows. My opinion is that this is one of the many flaws created by the insane A1 reference style, and the best way to attack this problem at the root is to simply switch to R1C1 at the beginning of the macro.
With Application
If .ReferenceStyle = xlA1 Then
.ReferenceStyle = xlR1C1
End If
End With
Some day, I plan to start a non-profit effort to rid the world of the mis-begotten $A4:B$3 craziness altogether. But too many people are used to A1. And if you send your boss or co-worker a spreadsheet with numbers for columns, she might get upset or confused.
So the other solution is to introduce some ADDRESS() elements into your SUMs. The ADDRESS function takes only numbers (well, string for sheet reference) and will return a cell reference in whichever style you want.
For i=0 to Duration-1
Sheet2.Cells(RNumber+7+Duration, 5+2*i).Formula = _
"=SUM(INDIRECT(ADDRESS(" & RNumber + 6 & "," & 5+(2*i) & ",1,TRUE,""Sheet2""),1):" _
& "INDIRECT(ADDRESS(" & RNumber + 6 + Duration & "," 5+(2*i) & _
",1,TRUE,""Sheet2""),1))"
Next i
Or you can use Application.WorksheetFunction.Address() and avoid having the INDIRECT()s cluttering up your fomulas.