How can I convert this IF statement to use in VBA - excel

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])"

Related

Drag formula to the last row (take first cell as A6)

I need to fill the formula till the last used row. I have data from A6:A28, however while using below code its dragging the formula till row 628. Issue is with 4th line of the code. Please help me correct.
Dim Lastrow As Long
Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("H6:M6").Select
Selection.AutoFill Destination:=Range("H6:M6" & Lastrow), Type:=xlFillDefault
Copy Down Formulas
A Quick Fix
Just remove the 6 after the M.
Selection.AutoFill Destination:=Range("H6:M" & LastRow), Type:=xlFillDefault
An Improvement
No Select
No variable
No AutoFill
With ActiveSheet ' improve!
With .Range("H6:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.Formula = .Rows(1).Formula
End With
End With

VBA unique formula with variable

I'm having a really hard time setting the correct formula in VBA to output a list of unique values in a list.
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Range("B1").Select
ActiveCell.Formula2R1C1 = "=UNIQUE(RC[-1]:" & lastRow & ")"
Pretty simple what I'm trying to do. Just use the range from cell A1 to the last row in the that column and push it to the unique formula.
A1 notation:
Range("B1").Formula2 = "=UNIQUE(A1:A" & lastRow & ")"
R1C1 notation:
Range("B1").Formula2R1C1 = "=UNIQUE(RC[-1]:R[" & lastRow & "]C[-1])"

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 Formula to Last Row of a Single 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 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

Msgbox from formula result

I am trying to add a message box if the above formulas return an error or if the cells in the selection are blank, but as of yet have not been able to complete. Any ideas?
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("z2").Formula = "=VLOOKUP(A2,'Ship to'!$B:$C,2,0)"
Range("z2").AutoFill Destination:=Range("z2:z" & LastRow)
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Thanks again for your help
Phill
Use the IsError method to check if a cell formula is returning an error:
For Each cell in Range("z2:z" & LastRow)
If IsError(cell) then
' do what you need to do
End If
Next cell

Resources