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

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)

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

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

When trying to read #NAME? Value from excel to a string gives Type mismatch error

I have a macro which reads a description string from excel and finds out the particular string is present in the data or not.
Sometimes the input file might contain a value like #NAME? in the cell. When the macro reaches this cell its just gives error type mismatch Run time error 13 on the following line.
I just wanted to ignore this line and continue the with the next line. How I should I give a validation (if). I'm using a 'do- loop until' for the loop. descriptionString is a string variable.
descriptionString = currentwrkbk.Worksheets(1).Cells(i, 1).Value
Use IsError, something like so
if iserror(currentwrkbk.Worksheets(1).Cells(i, 1).Value))....
you may want to iterate through valid cells only using SpecialCells method of Range object, that allows you to filter a range returning a range with "all the cells that match the specified type and value"
for instance
myRange.SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues)
will return myRange cells containing constant (i.e. not resulting from formulas) text values only
so that a possible snippet for your purposes could be the following:
Option Explicit
Sub main()
Dim cell As Range
Dim currentwrkbk As Workbook
Dim descriptionString As String
With currentwrkbk.Worksheets(1)
For Each cell In .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues) '<--| loop through text values of column "A" only
descriptionString = cell.value
Next cell
End With
End Sub
#Nathan_Sav has alredy given you one option. Here is another.
If Not CVErr(currentwrkbk.Worksheets(1).Cells(i, 1).Value) = CVErr("2029") Then _
descriptionString = currentwrkbk.Worksheets(1).Cells(i, 1).Value
Here is the rest of the list
2007 returns a #DIV/0! error.
2042 returns a #N/A error.
2029 returns a #NAME? error.
2000 returns a #NULL! error.
2036 returns a #NUM! error.
2023 returns a #REF! error.
2015 returns a #VALUE! error.
Why not use IsError along with Clng to convert to an error code and test for that
Option Explicit
Sub Test()
Dim rng As Excel.Range
Set rng = ThisWorkbook.Worksheets(1).Range("i13")
'* i creat a #NAME here
rng.Formula = "=namenoexist"
Debug.Assert IsHashName(rng)
End Sub
Function IsHashName(ByVal rng As Excel.Range) As Boolean
Debug.Assert Not rng Is Nothing
Debug.Assert rng.Rows.Count = 1
Debug.Assert rng.Columns.Count = 1
Dim v As Variant
v = rng.Value2
'*So test for IsError and then cast to a long and test for 2029 to see if i
If IsError(v) Then
If CLng(v) = 2029 Then
IsHashName = True
End If
End If
End Function

CountIf and xlCellTypeVisible producing 1004 error

I'm trying to count the number of fields that contain the word "Change", but this is being performed on a filtered list, so I am using the SpecialCells(xlCellTypeVisible) property to test whether they are part of the filtered list. The sample code is shown below:
aWorkbook.Worksheets("Sheet1").Cells(22, 10).Formula = WorksheetFunction.CountIf(Range(Cells(3, 10), Cells(20, 10)).SpecialCells(xlCellTypeVisible), "Change")
But I am receieving the following error:
Run-time error'1004': Unable to get the CountIf property of the WorksheetFunction class
The following code, using Sum, works as expected though, which I find very odd
aWorkbook.Worksheets("Sheet1").Cells(22, 7).Formula = WorksheetFunction.Sum(Range(Cells(3, 7), Cells(20, 7)).SpecialCells(xlCellTypeVisible))
Public Sub CountValue()
Dim rngValues as Range
Dim varCounter as Variant
Dim counter as Byte
Set rngvalues = ThisWorkbook.Sheets("Sheet1").Range("$J$3:$J$20")
For Each varCounter in rngvalues.SpecialCells(xlCellTypeVisible)
If varCounter = "Change" Then
counter = counter + 1
End if
Next
ThisWorkbook.Sheets("1").Range("$J$22").Value = counter
End Sub