VBA: Dims inside a Vlookup - vba

I'm trying to use a Vlookup with the line:
wRange.FormulaR1C1 = "=VLOOKUP(RC[-" & (vCol - 1) & "],'" & vSheet & "'!C[15]:C[16],2,FALSE"
where vCol as Integer = 9 and vSheet as String = 082015
So basically, that line can be rewritten as:
wRange.FormulaR1C1 = "=VLOOKUP(RC[-8],'082015'!C[15]:C[16],2,FALSE)"
However, I'm getting an "Application-defined or object-defined error" during run-time.
Do you see what I'm doing wrong?
Thanks.

"=VLOOKUP(RC[-" & (vCol - 1) & "],'" & vSheet & "'!C[15]:C[16],2,FALSE)"
Note missing bracket at the end

Related

IF conditional formula in VBA - Run-time error '1004' [duplicate]

This question already has an answer here:
Different languages issue when inserting formula from VBA
(1 answer)
Closed 1 year ago.
I am getting a Run-time error '1004' at the line with IF statement. Checked the previous posts, but could not find a relevant solution.
My code is
Sub RefreshFormulae()
Set sh = ActiveSheet
lastR = sh.Range("B" & sh.Rows.Count).End(xlUp).Row
sh.Range("$J$3:$K$" & lastR).ClearContents
sh.Range("$H$3:$H$" & lastR).Formula = "=$G3"
sh.Range("$K$3:$K$" & lastR).Formula = "=IF($I3<>""Not Found"";$I3;"""")"
End Sub
Error is in the line below. Am I missing anything here?
sh.Range("$K$3:$K$" & lastR).Formula = "=IF($I3<>""Not Found"";$I3;"""")"
You need to use a comma (,) instead of a semicolon (;) in VBA, as the regional format for separators inside functions only works in the Excel formula bar interface, not macros/VBA.
Try:
"=IF($I3<>" & Chr(34) & "Not Found"",$I3,"""")"
This would make the whole block change to:
Sub RefreshFormulae()
Set sh = ActiveSheet
lastR = sh.Range("B" & sh.Rows.Count).End(xlUp).Row
sh.Range("$J$3:$K$" & lastR).ClearContents
sh.Range("$H$3:$H$" & lastR).Formula = "=$G3"
sh.Range("$K$3:$K$" & lastR).Formula = "=IF($I3<>" & Chr(34) & "Not Found"",$I3,"""")"
End Sub
You need to use commas when entering formulas in VBA, even when you use semicolon directly in the sheet.
Sub RefreshFormulae()
Set sh = ActiveSheet
lastR = sh.Range("B" & sh.Rows.Count).End(xlUp).Row
sh.Range("$J$3:$K$" & lastR).ClearContents
sh.Range("$H$3:$H$" & lastR).Formula = "=$G3"
sh.Range("$K$3:$K$" & lastR).Formula = "=IF($I3<>""Not Found"",$I3,"""")"
End Sub

vba formulaR1C1 concatenate from different columns with function

I'm getting error for the following code: Application-defined or object-defined error.
How do I fix this?
p.s. lastRow is a number
Range("A2:" & "A" & lastRow).FormulaR1C1 = "=CONCATENATE(RC[6],RC[7],RC[9],RC[10],RC[11],ROUND(RC[12],0),IF(RC[13]="",0,RC[13]),ROUND(RC[14],2),YEAR(RC[15]),MONTH(RC[15]),DAY(RC[15]))"
Your "" need to be """"
Range("A2:" & "A" & 3).FormulaR1C1 = "=CONCATENATE(RC[6],RC[7],RC[9],RC[10],RC[11],ROUND(RC[12],0),IF(RC[13]="""",0,RC[13]),ROUND(RC[14],2),YEAR(RC[15]),MONTH(RC[15]),DAY(RC[15]))"

VBA: Using Dims in a Vlookup

I'm trying to do a vlookup in VBA and I'm getting an Application-defined or object-defined error run-time error.
I rewrote the line with integers and strings instead of Dims and it works fine, but I need to make it variable.
'Throws Error
rc = -6
tempwb = "Supplier Master - Location - 08-13-15.xls"
acol = 1
zcol = 14
wRange.FormulaR1C1 = "=VLOOKUP(RC[rc],'[" & tempwb & "]Sheet1'!C" & acol & ":C" & zcol & "," & ((zcol - acol) + 1) & ",FALSE)"
'Works
wRange.FormulaR1C1 = "=Vlookup(RC[-6],'[Supplier Master - Location - 08-13-15.xls]Sheet1'!C1:C14,14,FALSE)"
try changing
wRange.FormulaR1C1 = "=VLOOKUP(RC[rc],
to be
wRange.FormulaR1C1 = "=VLOOKUP(RC[" & rc & "],
Done?
It was easy enough to diagnose by doing this:
Dim sFormula As String
sFormula = "=VLOOKUP(RC[rc],'[" & tempwb & "]Sheet1'!C" & acol & ":C" & zcol & "," & ((zcol - acol) + 1) & ",FALSE)"
Debug.Print sFormula
wRange.FormulaR1C1 = sFormula
Try changing your string so you include the rc variable.
wRange.FormulaR1C1 = "=VLOOKUP(RC[" &rc&"],'["

VBA SumProduct Runtime Error 13

As a part of a much longer code, I am trying to include a SumProduct
Dim SumPr as Variant
SumPr=Application.WorksheetFunction.SumProduct(((Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("A:A")) = Range("A" & i)) * ((Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("D:D")) = "PH") * (Workbooks(Source2).Sheets("Prices_EUR_Adj").Range(ColLtr & ":" & ColLtr)))
MsgBox SumPr
However, I keep getting Runtime Error 13 for some reason. Any idea what is wrong? Source2 is properly defined, and ColLtr is letter conversion of column number I got from match; it also works ok as checked by message box.
Try using the Evaluate function instead:
Dim SumPr As Variant
SumPr = Application.Evaluate("SUMPRODUCT(--(" & Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("A:A").Address & "=" & Range("A" & i).Address & ")," & _
"--(" & Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("D:D").Address & "=""PH"")," & _
Workbooks(Source2).Sheets("Prices_EUR_Adj").Range(ColLtr & ":" & ColLtr).Address & ")")
MsgBox SumPr
I'm sure the WorksheetFunction.SumProduct will also work, but I was able to get the Evaluate function working easier.

Using string variables within a formula in VBA

I do not understand why this code doesn't work:
Cells(i, formula_col_index).Value = "=IF(" & time_location & "<>" & time_benchmark & ",""ERROR"",""OK"")"
where
time_location=" 17:00:00",
time_benchmark=" 17:30:00"
It keeps throwing application-defined (or object-defined) error.
Thanks in advance.
Since your variables time_location and time_benchmark contains string values, you should include them in double quotes when using formula:
Cells(i, formula_col_index).Value = "=IF(""" & time_location & """<>""" & time_benchmark & """,""ERROR"",""OK"")"