Auto-filing Formula to Last Row of a Single Column - excel

I need to constantly start at cell R2 and auto-fill a formula down to the last row of column R. However, the number of rows is constantly changing so I need to write a macro that finds the last row and stops their. I've tried this code but keep getting errors. Any thoughts?
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("R" & Rows.Count).End(xlUp).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)

The error you are getting is due to this Range("R2" & Lastrow)
The range address concatenates to R21400 if the last row was 1400. So we need to add the :R to the address.
Range("R2:R" & Lastrow)
It will now concatenate to R2:R1400
There is not need to do it with three lines, one will suffice:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P" & Rows.Count).End(xlUp).Row
Range("R2:R" & Lastrow).FormulaR1C1 = "=RC[-2]/RC[-4]"
End Sub

Not sure if you already tried this:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P2").End(xlDown).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
It seems to me that Range("R" & Rows.Count) locates you out of current region

Related

How can I convert this IF statement to use in VBA

I have the following IF statement =IF(LEN(F2)>LEN(H2),F2,H2)
This just checks which is longer between F2 and H2 and populates I2 with the longest. When I put this in VBA it comes out as =IF(LEN(G1048558)>LEN(I1048558),G1048558,I1048558)in my spreadsheet
How could I fix this?
Sub PopulateI()
Range("I2").Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(R[-20]C[-2])>LEN(R[-20]C),R[-20]C[-2],R[-20]C)"
Selection.AutoFill Destination:=Range("I2:I" & Range("F" & Rows.Count).End(xlUp).Row)
End Sub
Skip the AutoFill and write the formula to the entire range in one step.
If it's easier, you can use the A1-style formula instead of the current R1C1-style formula.
Dim lastRow As Long
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("I2:I" & lastRow).Formula = "=IF(LEN(F2)>LEN(H2),F2,H2)"
Or as noted by #Scott Craner, you can fix the R1C1-style formula (which is the real issue):
Range("I2:I" & lastRow).FormulaR1C1 = "=IF(LEN(RC[-3])>LEN(RC[-1]),RC[-3],RC[-1])"

How to change destination ranges when using autofill in VBA

I am quite new to VBA, I am making a macro where it copies data from 'Sample' workbook to 'Etracker' workbook. After copying cell "H11" from 'Sample' to the last row of C column in Etracker, I want C column to autofill down to until where data is in J column. the code works but where it says "C16926" now, it changes every time because I constantly entering data into it. so I want to autofill it from last row in column C all the time.
I tried different ways to change "C16926" to vary but it doesn't seem to work.
Please help! and thanks in advance.
LastrowC = Etracker.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
LastrowJ = Etracker.Cells(Rows.Count, 10).End(xlUp).Offset(1, 0).Row
Sample.Activate
Range("H11").Copy
Etracker.Cells(LastrowC, 3).PasteSpecial xlPasteValues
Etracker.Activate
Etracker.Range("C" & LastrowC).Select
Selection.AutoFill Destination:=Range("C16926:C" & Range("J" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy
You just need to concatenate your last row into the address like
"C" & LastrowC & ":C" & Range("J" & Rows.Count).End(xlUp).Row)
Note that you don't need to .Select or .Activate if you specify a worksheet for every Range, Cells, Rows and Columns object. Using .Select or .Activate is very unreliable and makes your code really slow.
So you should end up with something like
Dim LastRowC As Long
LastrowC = Etracker.Cells(Etracker.Rows.Count, "C").End(xlUp).Offset(1, 0).Row
Dim LastrowJ As Long
LastrowJ = Etracker.Cells(Etracker.Rows.Count, "J").End(xlUp).Offset(1, 0).Row
Sample.Range("H11").Copy
Etracker.Cells(LastrowC, "C").PasteSpecial xlPasteValues
Etracker.Range("C" & LastrowC).AutoFill Destination:=Etracker.Range("C" & LastrowC & ":C" & LastrowJ), Type:=xlFillCopy

Select multiple ranges with VBA

I need to select multiple ranges in a worksheet to run various VBA code on them. The ranges will always begin on row 84 but the end depends on how far down the data goes. I've been selecting these ranges separately using code like this:
Sub SelectRange()
Dim LastRow As Integer
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A84:B" & LastRow).Select
End Sub
That works fine, but I can't figure out how to select multiple ranges at once. I've tried everything I can think of:
Range("A84:B", "D84:E", "H84:J" & LastRow).Select
Range("A84:B,D84:E,H84:J" & LastRow).Select
Range("A84:B & LastRow,D84:E & LastRow,H84:J & LastRow").Select
Nothing works. I get a run-time error when running any of those.
Use UNION:
Dim rng as Range
With ActiveSheet
set rng = Union(.Range("A84:B" & LastRow),.Range("D84:E" & LastRow),.Range("H84:J" & LastRow))
End With
rng.select
But if you intend on doing something with that range then skip the .Select and just do what is wanted, ie rng.copy
Put your dis-continued range address in the first argument of Range object.
For example, Range("A:A,D:D").Select will select column A and column D.
In your case, you may try:
Dim str As String, LastRow As Integer
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
str = "A84:B" & LastRow & ",D84:E" & LastRow & ",H84:J" & LastRow
Range(str).Select
Range("A84:B & LastRow & "," & "D84:E & LastRow & "," & "H84:J & LastRow").Select

Application-defined or objected-defined error only shows with If Vlookup Statement

So i'm running into a problem with my script which fills down a formula to the last row of a column. It works for the most part except when it steps into my if function.
Sub vlookups()
'
' vlookups Macro
'
'
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("C2:C" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,2,false)"
Range("E2:E" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,3,false)"
Range("G2:G" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,4,false)"
Range("I2:I" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,5,false)"
Range("K2:K" & LastRow).Formula = "=IF(VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false)=0,'',VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false))"
End Sub
This runs fine on the first 4 Range vlookups, however when it hits the last one with the IF(VLOOKUP it shows an error. I'm not too well versed with VBA so i'm not sure what could be causing this issue.
Any help?
You need to double up on the quotes inside a formula:
"=IF(VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false)=0,"""",VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false))"

Auto-filing a formula to the last row of a column

I need to constantly start at cell R2 and auto-fill a formula down to the last row of column R. However, the number of rows is constantly changing so I need to write a macro that finds the last row and stops there. My code, as it stands right now, will auto-fill column R to the end of the worksheet (not to the row where my data stops). How do I get the auto-fill to stop at the correct row where there is no longer any data?
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P" & Rows.Count).End(xlDown).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2:R" & Lastrow)
End Sub
Try this (avoids select/activate):
Dim Lastrow As Long
'Lastrow = Range("P" & Rows.Count).End(xlDown).Row
Lastrow=Cells(Rows.Count, "P").End(xlUp).Row
Range("R2:R" & Lastrow).FormulaR1C1 = "=RC[-2]/RC[-4]"
set r = range("R2")
If r.Offset(1, 0) <> "" Then
lastRow = r.End(xlDown).row
Else
lasRow = r.row
End If

Resources