VBA If Value = Vlookup error

JonesKi

New Member
Joined
Oct 10, 2014
Messages
4
Hi Guys,

having a problem with some VBA.

Its working until the Vlookup returns a "#N/a" then i get a:
Run-time error '1004'
unable to get the vlookup property of the worksheet funtion class.

any ideas?

i need it to check a selection of cells and highligh if they match a list in workbook range it refers to.


Sub Exclude()
For Each X In Selection
Set wbk = Workbooks.Open("D:\Myfile")
Set range1 = wbk.Sheets(1).Range("A2:A829")
If X.Value = Application.WorksheetFunction.VLookup(X.Value, range1, 1, False) Then


With X.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.color = 2228049
.TintAndShade = 0
.PatternTintAndShade = 0

End With


End If

Next


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try

Code:
Sub Exclude()
For Each x In Selection
Set wbk = Workbooks.Open("D:\Myfile")
Set range1 = wbk.Sheets(1).Range("A2:A829")
v = Application.VLookup(x.Value, range1, 1, False)
If Not IsError(v) Then
If x.Value = v Then


With x.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 2228049
.TintAndShade = 0
.PatternTintAndShade = 0

End With


End If
End If
Next


End Sub
 
Upvote 0
Try

Code:
Sub Exclude()
Dim MyVal As Variant
Dim wbk As Workbook, range1 As Range, x As Long

Set wbk = Workbooks.Open("D:\Myfile")
Set range1 = wbk.Sheets(1).Range("A2:A829")
For Each x In Selection
    MyVal = Application.Match(x.Value, range1, 0)
    If Not IsError(MyVal) Then
        With x.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 2228049
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top