Application VLOOKUP no value - vba

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

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 1004 Error with named range formula

I am tinkering with a program but I get an error that I cant place.
Error is 1004
Code:
Source_1_Criteria = "Factuur"
Source_1_Name = Range("MDM_MDM_Tool_List").Find(what:=Source_1_Criteria).Offset(0, 2).Value
Source_1_Area = Range("MDM_MDM_Tool_List").Find(what:=Source_1_Criteria).Offset(0, 4).Value
ActiveWorkbook.Names.Add Name:=Source_1_Name, RefersTo:=Source_1_Area
The last line gives me the error.
I know its something in the 3 line.
The result in the 3 line results in error with current contents in the cell
=VERSCHUIVING(archief!$A$2;0;0;1;AANTALARG(archief!$A$2:archief!$Y$2))
When I change the contents to =archief!$A$2 the code works.
But when I manually copy/paste the formula in a named range it works....
So to my calculation:
Formula = Works
Code = Works
those 2 together gives
error 1004
The formula you should have in place of =VERSCHUIVING(archief!$A$2;0;0;1;AANTALARG(archief!$A$2:archief!$Y$2)) should be this one:-
=ADRES(2;1;;;"Archief") & ":" & ADRES(2;AANTALARG(Archief!$A$2:$Y$2))
Below is code which I tested thoroughly. It works with the formula I supplied yesterday (above). You can use Edit/Replace to restore your original variable names in the code below.
Option Explicit
Private Sub Dutch()
' 05 Jan 2018
Dim Wb As Workbook
Dim Ws As Worksheet
Dim SrcCrit As String
Dim SrcName As Variant
Dim SrcArea As String
Dim Sp() As String
Dim Rng As Range
Set Wb = ActiveWorkbook ' could be ThisWorkbook
Set Ws = ActiveSheet ' could be another sheet
SrcCrit = "Factuur"
Set Rng = Ws.Range("ToolList").Find(What:=SrcCrit)
If Not Rng Is Nothing Then
SrcName = Rng.Offset(0, 2).Value
' Debug.Print SrcName
SrcArea = Rng.Offset(0, 4).Value
' Debug.Print SrcArea
Sp = Split(SrcArea, "!")
If UBound(Sp) Then
If Left(Sp(0), 1) = "'" Then Sp(0) = Mid(Sp(0), 2)
If Right(Sp(0), 1) = "'" Then Sp(0) = Left(Sp(0), Len(Sp(0)) - 1)
Else
Sp = Split(Ws.Name & "|" & Sp(0), "|")
End If
Set Rng = Wb.Worksheets(Sp(0)).Range(Sp(1))
' Debug.Print Rng.Worksheet.Name, Rng.Address
On Error Resume Next
Wb.Names(SrcName).Delete
On Error GoTo 0
Wb.Names.Add Name:=SrcName, RefersTo:=Rng
' With Wb.Names(SrcName)
' Debug.Print .Name, .RefersTo
' End With
End If
End Sub
I found that error 1004 is a little complicated in the above context because such an error will occur when a faulty Name exists in the workbook, such as may have been created by your original code. In that case the error indicates the existence of an unrecognised element in the workbook which may be totally unrelated to the code you are intending to test run. 1004 errors of this nature are commonly caused by compatibility issues, not even related to names. Google for _xlfn IERROR
I got a persistent 1004 error while developing the above code because I had named the offsets from the found range as "first" to "fourth" which allocated "second" to the new named range. Wb.Names("Second").Name correctly returned "second" but Wb.Names(ScrName).Name trigered error 1004 - just your complaint. The error went away when I found out that SECOND() is a worksheet function and changed the name in the offset cell.

How to append address of current cell to range object

I've been trying to write a function that goes through an Excel worksheet to find a range of cells fulfilling a certain condition (two cells in the same row that have to be equal).
I've written the following code that goes through the Worksheet row by row and checks if the condition is fulfilled.
If a cell is found for which the condition is true I would like the address of the cell to be added to a range.
The output of the function should finally be this range which is subsequently used to populate a dropdown menu in a dialog with the entries fulfilling the condition.
Private Function DetermineRange(WorksheetName As String, Column1 As Integer, Column2 As Integer) As Range
Dim rng As Range
'Go through rows of specified worksheet
For currRow = 1 To Worksheets(WorksheetName).Cells(Rows.Count, 3).End(xlUp).Row
'Compare cells in specified columns of current row
If Worksheets(WorksheetName).Cells(currRow, Column1).Value = Worksheets(WorksheetName).Cells(currRow, Column2).Value _
And Not (Worksheets(WorksheetName).Cells(currRow, Column1).Value = "") Then
'If cells are equal, but not empty, append current adress of current cell to range
If Not rng Is Nothing Then
Set rng = Union(rng, Worksheets(WorksheetName).Cells(currRow, 2))
Else
Set rng = Worksheets(WorksheetName).Cells(currRow, 2)
End If
End If
Next currRow
If Not rng Is Nothing Then
'return found Range
Set DetermineRange = rng
MsgBox ("Range is: " & rng)
Else
'DEBUG: Throw error message if rng is empty,
MsgBox ("DEBUG DetermineRange Function:" & vbCrLf & _
"Error! No corresponding Cells found in Sheet" & WorksheetName)
End If
End Function
Cycling through the rows works fine, however I don't seem to be able to add the addresses for the cells after the condition is checked to the range object.
I have also tried the following, which results in a
Runtime error 424: Object required
'If cells are equal, but not empty, append current address of current cell to range
If Not rng Is Nothing Then
Set rng = Union(rng, Worksheets(WorksheetName).Cells(currRow, 2).Address)
Else
Set rng = Worksheets(WorksheetName).Cells(currRow, 2).Address
End If
I've been looking around, but can't seem to find much information on how to add cells to range objects however...
Maybe one of you could help! Any kind of pointer in the right direction is highly appreciated!
Thanks in advance for any kind of help!
Edit:
I am calling the function like this:
Set NameRng = DetermineRange("Features", ProjectColumn, TCGroupColumn)
cb_FcnName.RowSource = Worksheets(3).Name & "!" & NameRng.Address
But I get the following error:
Runtime Error 380: Not able to set property RowSource
One method is to capture the cell addresses. Concatenate these and use the final value to build a new range.
Example:
Public Function DetermineRange(WorksheetName As String, Column1 As Integer, Column2 As Integer) As Range
Dim rng As Range
Dim currRow As Integer
Dim targetSheet As workSheet ' Shortcut to requested sheet.
Dim matchesFound As String ' Address of matching cells.
' This line will raise an error if the name is not valid.
Set targetSheet = ThisWorkbook.Sheets(WorksheetName)
'Go through rows of specified worksheet
For currRow = 1 To targetSheet.UsedRange.Rows(targetSheet.UsedRange.Rows.Count).Row
'Compare cells in specified columns of current row
If targetSheet.Cells(currRow, Column1).Value <> "" Then
If targetSheet.Cells(currRow, Column1).Value = targetSheet.Cells(currRow, Column2).Value Then
' Capture address of matching cells.
If Len(matchesFound) > 0 Then
matchesFound = matchesFound & "," & targetSheet.Cells(currRow, Column1).Address
Else
matchesFound = targetSheet.Cells(currRow, Column1).Address
End If
End If
End If
Next currRow
' DEBUG: Throw error message if no matches found.
If Len(matchesFound) = 0 Then
Err.Raise vbObjectError + 101, "DetermineRange", "No matching cells found."
End If
' Return range.
Set DetermineRange = targetSheet.Range(matchesFound)
End Function
The code is a little rough and ready. I can't help but feel there are few too many lines. But the basic approach works.

VLOOKUP generates Run-Time Error '1004'

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!

Run time error 1004 on using Vlookup function

Im trying to highlight range of cells when a date is present in the list of holidays. But on running the below code, the Run time error 1004 is displayed. I have even tried handling it in error handler; but it is not working. Can somebody please help me why this error is occuring and resolve the same?
Sub highlight_cells()
Dim myrange As Range
On Error GoTo myerr:
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
temp = Application.WorksheetFunction.VLookup(Range(Cells(1, i)), [holidays], 2, False)
If (Application.WorksheetFunction.IsNA(temp)) Then
myrange.Interior.Color = 3
End If
Next i
myerr:
If Err.Number = 1004 Then
MsgBox "vlookup error"
End If
End Sub
Range(Cells(1, i)) isn't a valid range reference
maybe you wanted to reference Cells(1, i)
furthermore you can exploit the Application VLookup() method that wraps the possible error in the returned variant variable that you can check with IsError() function like follows:
Dim temp As Variant
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
temp = Application.VLookup(Cells(1, i), [holidays], 2, False)
If Not IsError(temp) Then Cells(1, i).Interior.Color = 3
Next i
Here is a conditional formatting method, without using VBA.
Select your range > Conditional Formating > New Rule > Use a formula ...
Enter this formula
=VLOOKUP($A2,$J$2:$K$6,1,FALSE)
Take care of the "$" in the formula. This should highlight all cells that were found in the holidays list.
Your code is okay , It worked in Excel 2010 , Your problem is with VBA Error handling method.
Go to Tools --> Options -->General --> Error Trapping
And check "Break on unhanded Errors"
sorry all these times I was referring to column 2 in vlookup. That was causing the problem. The list of holiday is a single column list. Hence vlookup was throwing error. ANd one more thing the named ranges work as I have entered and even the actual range also gives the same result.
Sub highlight_cells()
Dim myrange As Range
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
MsgBox Cells(1, i)
temp = Application.VLookup(Cells(1, i), [holidays], 1, False)
If Not IsError(temp) Then
myrange.Interior.ColorIndex = 3
End If
Next i
End Sub