Vlookup vba

kevin1970

New Member
Joined
Jun 2, 2011
Messages
11
Hi All
been a member for a while now, always been able to work problems out but cannot get this one to work. Please help.

I am setting up a check list for spares. From my form i have check the number is available button. on pressing the button it runs the code. i have a sheet called 'spares' and column 'A' is the part number i am trying to look up. When i run to code its asked me for the part number to check and the ends and give an error '1004' unable to get vlookup property of worksheet function class. can anyone help.

Private sub Check_click()
Dim part number as range
Dim number as variant
Dim partnumber as variant
Sheets("spares").activate
set part number = sheets("spares").Range("a1:a135")
partnum=inputbox("enter number to check ")
Number=worksheetfunction.vlookup(partnum,partnumber,1,false)
msgbox partnum & "is available " & number
end sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Maybe

Code:
Private Sub Check_click()
Dim partnumber As Range
Dim number As Variant
Dim partnumber As Variant
Sheets("spares").Activate
Set partnumber = Sheets("spares").Range("a1:a135")
partnum = Val(InputBox("enter number to check "))
number = Application.VLookup(partnum, partnumber, 1, False)
MsgBox partnum & "is available " & number
End Sub
 
Upvote 0

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
4,142
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Code:
Private Sub Check_click()
    Dim partnumber As Range
    Dim number As Variant
    Dim partnum As Integer
    Sheets("spares").Activate
    Set partnumber = Sheets("spares").Range("a1:a135")
    partnum = InputBox("enter number to check ")
    number = WorksheetFunction.VLookup(partnum, partnumber, 1, False)
    MsgBox partnum & "is available " & number
End Sub

Although if a match is not found by vlookup, it throws up an error, so you might need an error handler
 
Last edited:
Upvote 0

kevin1970

New Member
Joined
Jun 2, 2011
Messages
11
Maybe

Code:
Private Sub Check_click()
Dim partnumber As Range
Dim number As Variant
Dim partnumber As Variant
Sheets("spares").Activate
Set partnumber = Sheets("spares").Range("a1:a135")
partnum = Val(InputBox("enter number to check "))
number = Application.VLookup(partnum, partnumber, 1, False)
MsgBox partnum & "is available " & number
End Sub


that work thanks.

Now what I don't know how to do is if the part number in not in the spares sheet I want to return 'part not available' do you know how I can do it?
thanks kev
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Private Sub Check_click()
Dim partnumber As Range
Dim number As Variant
Dim partnumber As Variant
Sheets("spares").Activate
Set partnumber = Sheets("spares").Range("a1:a135")
partnum = Val(InputBox("enter number to check "))
number = Application.VLookup(partnum, partnumber, 1, False)
If IsError(number) Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox partnum & "is available " & number, vbInformation
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,092
Messages
5,984,600
Members
439,896
Latest member
SquareCare

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
Top