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

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

Related

for loop over sheets in a workbook with formulas until the end of a column with data

I have have the following problem with vba and would like to ask you for help.
I hope I haven't overseen a similar question in the froum
I have several sheets in an excel workbook. On each sheet I would like to run the same formulas. The formula gets its data from the cell C3 on the same sheet and should run until the end of the data in column C xxx . The length of the data is different on each sheet. Each sheet has its own data set.
The code I have written works fine in the sense that it works itself through through the sheets starting from sheet 2 until the end of the sheets.
The code gets executed via an icon in the toolbar.
On each sheet I have values what the formulas should use. The values in column C are different long. Some have for example just 15 some have over 300000.
For example, if I press the icon when I'm on sheet 1 with no data in column C the macro takes the first 15 values/range of the column for the formulas in all sheets. Which means I miss all values from for example at sheet 3 with 300000 values. If I am on sheet 3 and press the icon there I takes the 300000 values/range and uses the range of the 300000 values in all the other sheets.
If I run the code without the loop on each sheet it works fine. It selects the right range with values of the column.
There are no empty cells in the column.
Has anyone an idea what is wrong in the code that it does not select the real amount of values in column C for the different sheets?
I work on a mac with excel 14.
The following code is what I have so far:
Sub Start()
Dim i As Long
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To Worksheets.Count
With Sheets(i)
.Range("K11").Formula = "=COUNT(C3:C" & lastrow & ")"
.Range("K12").Formula = "=MEDIAN(C3:C" & lastrow & ")"
.Range("K13").Formula = "=AVERAGE(C3:C" & lastrow & ")"
.Range("K14").Formula = "=MIN(C3:C" & lastrow & ")"
.Range("K15").Formula = "=MAX(C3:C" & lastrow & ")"
.Range("K16").Formula = "=STDEVP(C3:C" & lastrow & ")"
End With
Next i
End Sub
You have to use .Cells to make it refer to the object in the With statement, otherwise it refers to the current sheet:
Sub Start()
Dim i As Long
For i = 2 To Worksheets.Count
With Sheets(i)
lastrow = .Cells(Rows.Count, 3).End(xlUp).Row
Debug.Print (i)
Debug.Print (lastrow)
.Range("K11").Formula = "=COUNT(C3:C" & lastrow & ")"
.Range("K12").Formula = "=MEDIAN(C3:C" & lastrow & ")"
.Range("K13").Formula = "=AVERAGE(C3:C" & lastrow & ")"
.Range("K14").Formula = "=MIN(C3:C" & lastrow & ")"
.Range("K15").Formula = "=MAX(C3:C" & lastrow & ")"
.Range("K16").Formula = "=STDEVP(C3:C" & lastrow & ")"
End With
Next i
End Sub

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

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

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