VLOOKUP generates Run-Time Error '1004' - vba

I want to setup a template that finds data based on pasted data in another worksheet.
Private Sub GoNoGo()
Dim i As Integer
Dim OffInt As Integer
Dim Neg As Integer
Neg = -30
Dim Ret As String
Dim I3 As Cell
Dim FindValue As String
Worksheets("BF59520").Activate
Range("AE3").Activate
i = 3
OffInt = 0
Do Until ActiveCell.Offset(0, Neg).Value = ""
If ActiveCell.Offset(0, -1).Interior.Color = RGB(255, 235, 160) Then
ActiveCell.Offset(1, 0).Activate
i = i + 1
Else
ActiveCell.Value = Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -18), Worksheets("Go No Go").Range("B2:O180"), 4, False)
ActiveCell.Offset(1, 0).Activate
i = i + 1
End If
OffInt = OffInt + 1
Loop
End Sub
When the loop gets to the VLOOKUP Line the code returns an error of Run-Time error '1004':
Unable to get the VLOOKUP property of the worksheetFunction class.

Generally, when you get that error on a Worksheet Function it means the function itself has returned an error. Make sure you're passing it the right values. If you can't guarantee that you'll get a correct value from the function then you can try using On Error like so
On Error Resume Next
Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -18), Worksheets("Go No Go").Range("B2:O180"), 4, False)
On Error GoTo 0
or you can capture the error in an evaluate statement like so
ActiveCell.Value = Evaluate("=IFERROR(VLOOKUP(" & ActiveCell.Offset(0,-18) & ", 'Go No Go'!B2:O180, 4, FALSE),0)")
The first will result in a no change in the ActiveCell when the vlookup fails, the second allows you to set a default value as the second argument of the 'IFERROR' function.
Hope this helps!

Related

Cell value from offset range

yellow_cell = ActiveCell.Address
MsgBox (Range(yellow_cell).Value)
implant = yellow_cell.Offset(6, -2).Address
MsgBox (Range(implant).Value)
The first MsgBox works, but the second one doesn't (Run-time error 424, object required).
I've also tried this:
implant = ActiveCell.Offset(6, -2).Address
MsgBox (Range(implant).Value)
And I get a run-time error 1004, Method 'Offset' of object 'Range' failed.
Anyone know what I'm doing wrong? I've unmerged all cells btw.
Here's the proper way to achieve the desired results:
Dim implant As Range, yellow_cell As Range
Set yellow_cell = ActiveCell
MsgBox yellow_cell.Value
Set implant = yellow_cell.Offset(6, -2)
MsgBox implant.Value
Notice: If the active cell is less than two columns away from column A, then this code will result in run-time error 1004, due to the second parameter of the Offset function.
Another way to achieve this is to test whether the column is in fact further right than 2 columns, something like below, this shouldn't cause any errors:
Sub foo()
Dim yellow_cell As String, implant As String
Dim col As Long
yellow_cell = ActiveCell.Address
MsgBox (Range(yellow_cell).Value)
col = ActiveCell.Column
If col > 2 Then
implant = ActiveCell.Offset(6, -2).Address
MsgBox (Range(implant).Value)
Else
MsgBox "Out of Range!", vbInformation, "Error"
End If
End Sub

Excel VBA VLookup Run-time error '1004'

I want to consolidate two sheets. In Tabelle 3 is already some data. Therefore, I defined the Next Free Row (NFR) and want my data from Tabelle 5 added to the next free row in Tabelle 3. Therefore, I wrote the following VLookup function.
Sub ConsolidateData()
Dim lastrow As Long
Dim NFR As Long
lastrow = Tabelle5.Range("A" & Rows.Count).End(xlUp).Row
NFR = Tabelle3.Range("A" & Rows.Count).End(xlUp).Offset(-3).Row
Set myrange = Tabelle5.UsedRange
For i = 4 To lastrow
Tabelle3.Cells(NFR + i, 1) = Application.WorksheetFunction.VLookup(Tabelle5.Cells(i, 1), myrange, 1, False)
Tabelle3.Cells(NFR + i, 2) = Application.WorksheetFunction.VLookup(Tabelle5.Cells(i, 1), myrange, 2, False)
Next i
End Sub
Even though, I'm already using this code in a different workbook, where it works smooth, it doesn't work here. Instead Run-time error '1004' occurs for this line:
Tabelle3.Cells(NFR + i, 1) = Application.WorksheetFunction.VLookup(Tabelle5.Cells(i, 1), myrange, 1, False)
Does anyone see the mistake or can tell me what I've coded wrong?
It seems that Vlookup cannot find value you are looking for and therefore throws an error. Application.WorksheetFunction.VLookup will return error '1004' if value cannot be found. Please consider following test:
Put in cell A2 value of 15.
Run below macros
macro 1:
Sub test1()
check = Application.WorksheetFunction.VLookup(15, Range("A1:A5"), 1, False)
Debug.Print check
End Sub
macro 2:
Sub test2()
check = Application.WorksheetFunction.VLookup(1, Range("A1:A5"), 1, False)
Debug.Print check
End Sub
As you can see second one throws an error. To overcome that issue you should change WorksheetFunction.VLoookup to Application.VLookup and implement error checking:
Sub test2()
If IsError(Application.VLookup(1, Range("A1:A5"), 1, False)) = False Then
check = Application.VLookup(1, Range("A1:A5"), 1, False)
End If
Debug.Print check
End Sub
Please look here as well: How to error handle 1004 Error with WorksheetFunction.VLookup?

Adding custom formula to Range() cell

Sub test()
Application.DisplayAlerts = False
Application.ScreenUpdating = False 'keeping the screen clean
Sheets("Data").Select
'Here is where the error is triggered.
With ThisWorkbook.Worksheets("TestSheet")
'.Range("A2:A" & Lr).Formula = "=CusVlookup(Z2,Data'!A:B,2)" <- this doesnt work either
.Range("A2:A" & Lr) = "=CusVlookup(Z2,Data'!A:B,2)"
End With
End Sub
Function CusVlookup(lookupval, LookupRange As Range, indexcol As Long)
Dim x As Range
Dim Result As String
Result = ""
For Each x In LookupRange
If x = lookupval Then
Result = Result & "," & x.Offset(0, indexcol - 1)
End If
Next x
CusVlookup = Result
End Function
I've tried using the regular Vlookup and works just fine but if I try to use this custom function it triggers the error. By the way I need to get the multiple matches "vlookups" into one cell separated by comma.(I added the goal just in case you know a better/faster way to do the same.)
whats wrong with the code?
Error 1004 - Application-defined or object-defined error

Application VLOOKUP no value

I am inputting data based on a VLOOKUP code but keep getting an error.
For Each Cell In Rng
Cell.Offset(0, 2).Value = Application.WorksheetFunction.VLookup(Cell, Table2, 1, False)
Next
I want Column C to either post the VLOOKUP value or return a message 'Returned Item Not Scanned'. I was using a error handler to do this however I keep getting an error when this runs.
'MyErrorHandler:
' If Err.Number = 1004 Then
' Cell.Offset(0, 2).Value = "Returned Item Not Scanned"
' ElseIf Err.Number = 13 Then
' MsgBox "Incorrect Exceptions Data."
' Else
'
' End If
Error is stating 'Unable to get VLookup Property of the WorksheetFunction Class.
Can anyone help?
Try the piece of code below, I am using Application.VLookup in order to trap the errors.
(When trapping the VLookup errors in this method, you are not getting 1004 for Err.Number)
Option Explicit
Sub VLookup_with_ErrHandling()
Dim Cell As Range
Dim Rng As Range
Dim Table2 As Range
' modify "Table2" range to your needs
Set Table2 = Sheets("Sheet1").Range("A1:C20")
' modify "Rng" range to your needs
Set Rng = Sheets("Sheet2").Range("A1:A10")
For Each Cell In Rng
If Not IsError(Application.VLookup(Cell, Table2, 1, False)) Then
Cell.Offset(0, 2).Value = Application.VLookup(Cell, Table2, 1, False)
Else
Cell.Offset(0, 2).Value = "Returned Item Not Scanned"
End If
Next
End Sub

Excel VBA: Error 1004 WorkSheetFunction 'Unable to get Vlookup property"

Trying to write a quick piece of VBA in Excel 2010 to
Use Vlookup to find a value
Return the value in the 3rd Column
Set a given cell to this value
My difficulty is with the formula.
Sub Metrics123()
Dim x As Integer
x = Application.WorksheetFunction.VLookup("Test", "A7:D9", 3, False)
Range("A1").Value = x
End Sub
When I run this I hit the error 1004: 'Unable to get the Vlookup Property of the WorksheetFunction
Any pointers appreciated!
Two ways for you.
1) Use .Formula property:
With ThisWorkbook.Worksheets("Sheet1").Range("A1")
.Formula = "=VLOOKUP(""Justin"",A7:D9,3,FALSE)"
.Value = .Value
End With
where .Value = .Value rewrites formula with it's result
2) use Application.VLookup with Range("A7:D9") instead "A7:D9":
Dim x
With ThisWorkbook.Worksheets("Sheet1")
x = Application.VLookup("Justin", .Range("A7:D9"), 3, False)
Range("A1").Value = x
End With
Note, that x should be Variant, because if nothing found, Application.VLookup returns Error 2042(#N/A)