How to use formular1c1 without defining the range in vba excel? - vba

My code is:
Range("M1").Select
ActiveCell.FormulaR1c1 = "SUM(R[2]C:R[4157]C)"
My problem is what if I have more than 4157 rows. How do I ensure all rows for that column will be added up?
Thanks!

Try with:
Range("M1").Select
ActiveCell.FormulaR1c1 = "=SUM(R[2]C:R[" & ActiveSheet.UsedRange.Rows.Count & "]C)"
EDIT:
Added the = to the formula, so that Excel understands it is a formula. Otherwise it would just put the text in the cell.

As per Jaycal's comment try this:
dim rowNum as integer
Range("M1").Select
rowNum=activesheet.cells(rows.Count,activecell.Column).end(xlUp).row
ActiveCell.FormulaR1c1 = "=SUM(R[2]C:R[" & rowNum & "]C)"
where using the cells notation means we can refer to row then column, row is taken as the max rows in worksheet, column is the active cell - which makes this a bit more reusable than referrring to range("M" & rows.count) given you've selected the cell anyway.

Related

r1c1 formula not working

I am trying to rank values using column J and tie breaker in column K with the result populating in column N. Column J & K are values.
Somehow it only generates one value wherever my cell is pointed at, which means if I run the vba codes at cell C19, it will just populate value 1 in C19, not from N6 where I want the results to be.
Here are my codes,
Sub test()
Dim LR1 As Long
LR1 = Range("J" & Rows.Count).End(xlUp).Row
With Range("N6:N" & LR1)
ActiveCell.FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
End With
End Sub
I am not sure what went wrong. I tried to do it manually using the excel formula and its working fine but not my vba codes.
ActiveCell is your issue.
Change
ActiveCell.FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
To .FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
Remove that and it should do what you want.
You will want to fully qualify your Range references that way they aren't depending on the ActiveSheet. This will provide you with consistent behavior and results.

Trying to copy a formula down an entire column to the last row of data in an adjacent column

I am a new VBA user, and I am trying to create a VBA code to copy a single Vlookup formula down an entire column to the last row of data in an adjacent column. I don't want to specify a specific range, because I intend to use this macro with multiple different files that have different row ranges, so I am looking for a code that will simply copy down to the last value in an adjacent column.
I have tried looking at other similar questions and answers on this website, but none of the solutions that I have found have been working, and I would really appreciate some help!
Here is my code that I currently have:
' Section5 Macro
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[6],'[PERSONAL.XLSB]Task and Sections'!R2C1:R254C2,2,FALSE)"
Range("C2").Select
Selection.Copy 'Copy Vlookup Formula
Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("C3").AutoFill destination:=Range("C3:C" & lastRow) 'Specify range for Column C based off of row count in Column B
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False 'Paste Vlookup equation down the column
End Sub
The formula I want to copy is "=VLOOKUP(RC[6],'[PERSONAL.XLSB]Task and Sections'!R2C1:R254C2,2,FALSE". The column that I want to copy this formula down is column C (in all rows except C1 which is the Header). The column that I want to refer to for row length is the adjacent column B.
Currently I am getting an error that says "Compile error: Named argument not found".
Any help would be greatly appreciated!
Thank you,
As pointed in the comment, you had a simple typo ("desination")... Nevertheless, your code doesn't seem to work even when this is fixed.
There's a much simpler approach. Try this:
Sub FillWithFormula()
Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("C2:C" & lastRow).FormulaLocal = "=B2*2"
End Sub
Notice that I replaced your formula with a simpler one (independent of external data) so I could verify that the routine works.
Here.
Sub thing()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
Range("C3:C" & lastRow).FormulaR1C1 = "=VLOOKUP(RC[6],'[PERSONAL.XLSB]Task and Sections'!R2C1:R254C2,2,FALSE)"
Range("C3:C" & lastRow).Value = Range("C3:C" & lastRow).Value
End Sub
Simpler, more elegant. Hasn't this kind of thing been solved like a million times all around the internet? Anyway, copy-paste is the slowest thing you can do in a macro. Avoid it. Just set the values of a range to be the values of the range. :)
Also, you can assign a formula to a whole range.

Macro to Auto Fill Down VLookUp Results down to last adjacent cell

I am trying to make a macro that will run a vlookup and then autofill down to the last adjacent cell that contains data. When I originally created the macro the dataset contained 1917 lines of data which is why you will continue to see it reference 1917. However, the dataset varies in size daily.
This particular macro takes a couple of steps before getting to this point:
Converts text to columns
Deletes an unnecessary column that is provided from a supporting report
Adds titles to 5 columns
Resizes all of the columns to fix the data
Then it runs the first vlookup which I need the data to fill down to the last adjacent cell to the left
Then it runs another vlookup which I need to also fill down to the last adjacent cell to the left
Here is the code for the two vlookup I am struggling with:
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-4],'CURRENT DAY'!R[-1]C[-4]:R[2498]C[1],5,0)"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-4],'CURRENT DAY'!R1C1:R2500C6,5,0)"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E1917")
Range("E2:E1917").Select
Columns("E:E").EntireColumn.AutoFit
Range("F2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-5],'CURRENT DAY'!R[-1]C[-5]:R[2498]C,6,0)"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-5],'CURRENT DAY'!R1C1:R2500C6,6,0)"
Range("F2").Select
Selection.AutoFill Destination:=Range("F2:F1917")
Range("F2:F1917").Select
There is not need to autofill. Just find the last row in column A and use that to denote the range in which to put the r1c1 formula
Dim lastrow As Long
lastrow = Range("A2").End(xlDown).Row
Range("E2:E" & lastrow).FormulaR1C1 = "=VLOOKUP(RC[-4],'CURRENT DAY'!R1C1:R2500C6,5,0)"
Range("F2:F" & lastrow).FormulaR1C1 = "=VLOOKUP(RC[-5],'CURRENT DAY'!R1C1:R2500C6,6,0)"
Columns("E:F").EntireColumn.AutoFit

Using SUMIF until last row in vba

I'm trying to incorporate a SUMIF formula into my macro. The code in excel looks like this: =SUMIF('WSO Interest' H2:H46, '20140618 Loans' D10, 'WSO Interest' S2:S46)
I set my dim as i and lastrow as integer
I already set that so that I can find the last row of the column and continue my loop until the last row.
I also used
Sheets("20140618 Loans").Select
Range("A10").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
to find the last row filled with text.
This is what
I have so far:
Range("W10").select
For i = 10 to lastrow
SUMIF formula would go here
Next i
So basically what I'm trying to do is use the SUMIF formula in my macro to start at W10 and keep calculating the SUMIF formula until it reaches the last row. Thanks for the help and feel free to ask any questions.
You can use the Formula property of a Range object to set the formula.
Range("W" & i).Formula = "=SUMIF('WSO Interest'!H2:H" & lastrow & _
", '20140618 Loans'!D10, 'WSO Interest'!S2:S" & lastrow & ")"
The & operator is used to concatenate strings together.
Dim r As Range
Set r = Range("W10", Range("W10").End(xlDown)) --Change it per requirement .....
For Each cell In r
Debug.Print cell.Address --Your Code goes here I guess
Next

#NAME error when setting cell formula through VBA.

I am writing a macro that requires me to get the average of the values in a column with an unknown number of rows. I use this to get the number of the last row:
Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
That works. What doesn't work is when I try to use it here:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE('table1'!AM2:AM" & lastRow & ")"
Doing that, I get a result of #NAME?. How can I fix this?
Change ActiveCell.FormulaR1C1 to ActiveCell.Formula since you're using basic A1 notation and not R1C1.