I have tried both sets of code below and either get runtime error 1004 or no answer if if put in error handling.
res.Cells(rlr, 8) = Application.WorksheetFunction.VLookup( _
res.Cells(rlr, 6), Repetition, 2, False)
And
res.Cells(rlr, 8).FormulaR1C1 = Evaluate("VLOOKUP(" & _
rec.Cells(2, 16) & "," & Repetition & ",2,FALSE)")
Repetition is a dynamic named range, I have tried manually doing this and it works fine.
What am I doing wrong?
Try:
res.Cells(rlr, 8).Value = res.Evaluate("VLOOKUP(" & _
res.Cells(2, 16).Address & "," & Repetition.Address & ",2,FALSE)")
Assuming rec.Cells(2,16) was a typo and all ranges are on sheet res
Related
When I was trying to throw a vertical bar "|" into my vlookup function, I got a 1004 error. I am not sure where it came from.
Initially the data table looks like this:
And my code was like this:
Sheets("Sheet 1").Cells(i, 2).Formula = "=VLOOKUP($A" & i & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Everything worked fine till the format of the data table got changed to:
So I had to throw the vertical bar "|" into my code. My new code was like this:
NewKey = Sheets("Sheet 1").Cells(i, 1).Value & "|" & Sheets("Data").Cells(i, 2).Value
Sheets("Sheet 1").Cells(i, 3).Formula = "=VLOOKUP(" & NewKey & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Apparently Excel 2010 did not like my codes and threw me a 1004 error. I am wondering if anyone knows a solution to this. Thank you in advance for your help.
NewKey needs to be in quotes when the formula is placed on the sheet, so you need to include the quotes in the string:
NewKey = Sheets("Sheet1").Cells(i, 1).Value & "|" & Sheets("Sheet1").Cells(i, 2).Value
Sheets("Sheet1").Cells(i, 3).Formula = "=VLOOKUP(""" & NewKey & """,Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
I'm new to VBA and encounter the
application-defined or object-defined error
while doing it.
Set twb = ThisWorkbook
Set extwbk = Workbooks.Open("abc.xlsx")
Set x = extwbk.Worksheets("FILTERED").Range("$A:$A")
With twb.Sheets("FILTERED")
For rw = 9 To .Cells(Rows.Count, 1).End(xlUp).Row
.Cells(rw, 34).Formula = "=IF(ISERROR(VLOOKUP(.Cells(rw, 1).Value2, x, 1, False))=TRUE,""New"",""Old"")"
Next rw
End With
This can be done manually but I'm asked to do it by programming. It is simply apply the formula from AH9 to the end of the data. May I know is there any coding error in the for loop?
Thanks in advance.
Dont put VBA variables inside your string formula. Resolve them in VBA and inject the result into the formula:
.Formula = "=IF(ISERROR(VLOOKUP(" & .Cells(rw, 1).Value2 & "," & _
x.Address(External:=True) & ", 1, False))=TRUE,""New"",""Old"")"
p.s. dont use =TRUE to check for a boolean, things like If ISERROR(Something) = TRUE don't make sense. Just If ISERROR(Something).
.Formula = "=IF(ISERROR(VLOOKUP(" & .Cells(rw, 1).Value2 & "," & _
x.Address(External:=True) & ", 1, False)),""New"",""Old"")"
If you want the formula to be dynamic and recalculate accordingly when the cell at (rw, 1) changes in the future, use .Address instead of .Value2:
' vvvvvvvv
.Formula = "=IF(ISERROR(VLOOKUP(" & .Cells(rw, 1).Address & "," & _
x.Address(External:=True) & ", 1, False)),""New"",""Old"")"
I am trying to use the excel VBA HLookup function. I have tried it two ways and both produce error. Could anyone explain me what is that I am doing wrong?
First try:
lookupValue = Worksheets(1).Name & "!A1"
tableArray = Worksheets(3).Name & "!$A$1:$" & Col_letter & "$1"
Worksheets("Comparison").Cells(1, 2).Value = "=HLookup(" & lookupValue _
& ";" & tableArray & ";1;FALSE)"
Second try:
tda = Worksheets(1).Cells(1, 1).Value ' I also tried using tda without .Value
Table = Worksheets(3).Range(Cells(1, 1))
Worksheets("Comparison").Cells(1, 2).Value = WorksheetFunction. _
HLookup(tda, Table, 1, False)
For your first one, you need to use US regional settings, so a comma separator, and you should really enclose the sheet names in single quotes in case they contain spaces or look like special names (e.g. dates):
lookupValue = "'" & Worksheets(1).Name & "'!A1"
tableArray = "'" & Worksheets(3).Name & "'!$A$1:$" & Col_letter & "$1"
Worksheets("Comparison").Cells(1, 2).Formula = "=HLookup(" & lookupValue _
& "," & tableArray & ",1,FALSE)"
and for the second you have to use a range object for the table argument:
tda = Worksheets(1).Cells(1, 1).Value ' I also tried using tda without .Value
Set Table = Worksheets(3).Range(Worksheets(3).Cells(1, 1), Worksheets(3).Cells(1, Col_letter))
Worksheets("Comparison").Cells(1, 2).Value = WorksheetFunction. _
HLookup(tda, Table, 1, False)
I have assumed Table is declared as Variant, Object or Range.
Note you will still get a run-time error if there is no match for your lookup value.
what you're doing there usually works, which is creating a string with the function you want to call and inputting it.
I don't think that's the safest way, since it would not work if you run that macro from an Excel with a different language.
the proper way to call an Excel function from VBA call is for example:
cells(1,2) = Application.WorksheetFunction.VLookup(123,Range("A1:C100"),3,FALSE)
anyway if you'd rather use this string approach the problem in the first try is that
"=HLookup(" & lookupValue _
& ";" & tableArray & ";1;FALSE)"
results in the string:
=HLookup(Sheet1!A1;Sheet3!$A$1:$B$1;1;FALSE)
note that you're using semicolons where you're supposed to use commmas.
the problem in the second try is that the .Range property takes a string as input, so you cant Range(Cells(1,1)) you should do something like .Range("A1:A3")
hope it helps !
I need help with my puzzle. I have written a code that works fine. Now a user wants to see not just the final result but also the formula written in VBA (for audit purposes) to verify that the results are OK. Is there a way to show BOTH results and formula in the same cell?
My code so far:
Sub Vlookup_Condition_VAT_table()
Dim Rng As Range
Dim i As Long
Application.ScreenUpdating = False
Workbooks("GST_recovery_overclaim.xlsm").Worksheets("MonthlyData_Raw").Activate
'Identify the Destination location to start populating vlookuped values
Range("AK2").Activate
With Worksheets("MonthlyData_Raw").Cells
Set Rng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
For i = 2 To Rng.Rows.Count
'populate the destination range with
'vlookup values from the list vlookup table
Rng.Cells(i, 37) = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)
Next
End With
Application.ScreenUpdating = True
End Sub
Thank you very much,
Russ
If you aren't looking at thousands of rows, you could try the following.
Instead of using this line to print the result of your vlookup formula:
Rng.Cells(i, 37) = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)
You could instead put the actual formula in the cell with this change
Rng.Cells(i, 37).Formula = "=VLOOKUP(" & _
.Cells(i, 28).Address & "," & _
"'" & Sheets("VAT_apportionment_table_201310").name & "'!D:G," & _
"4," & _
"False)"
--------------UPDATE BASED ON OP COMMENT-----------------------------
Adding error trapping via an ISERROR function (untested)
Dim errMsg as String
errMsg = "Not in exception list"
Rng.Cells(i, 37).Formula = "=ISERROR(VLOOKUP(" & _
.Cells(i, 28).Address & "," & _
"'" & Sheets("VAT_apportionment_table_201310").name & "'!D:G," & _
"4," & _
"False), " & errMsg & ")"
I created a variable for the error message for a couple of reasons.
Easier to debug later
Dealing with text in a formula that is being sent to the cell like this can be tricky. And using a variable gets around the trickiness.
Maybe add text to the result with the answer and the formula.
Rng.Cells(i, 37) = "Answer: " & Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False) & ", Formula: = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)"
If I undestood you correctly, you want to display the formula entered in a cell with the result. How about this?
'Assume that you have a Range("A1") equals to = cos(20)
Sub GetFormula()
Dim MyFormula As String
MyFormula = Replace(Range("A1").Formula, "=", "")
MsgBox ("Formula is : " & MyFormula _
& " Result is :" & Range("A1"))
End Sub
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.