User Form Lookup Error

ardykav

Board Regular
Joined
Oct 18, 2015
Messages
172
Office Version
  1. 365
Platform
  1. Windows
HI,

I have the below code and it keeps erroring out

What I am trying to do is in a userform write an asset ID in the txtpart box, once that comes up it then searches Stock tab for that asset ID in Column A and should then return whatever is in column B for that asset, next text box (.txtloc) field. Problem i have is though it keeps erroring and is saying the Stock.Range Bit is the problem, its says object variable or with block variabvle not defined

Any ideas?


VBA Code:
Private Sub Txtpart_AfterUpdate()
If WorksheetFunction.CountIf(Stock.Range("A:A"), Me.txtPart.Value) = 0 Then
MsgBox "Not in Stock"
Me.txtPart.Value = ""
Exit Sub
End If
With Me
.txtLoc = Application.WorksheetFunction.VLookup(CLng(Me.txtPart), Stock.Range("Lookup"), 2, 0)
End With
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Where do you declare and define the worksheet variable Stock ? That appears to be an object variable that is not defined.

You may need something like this at the start of your procedure...
VBA Code:
    Dim Stock As Worksheet
    Set Stock = Worksheets("Stock")
 
Upvote 0
Thanks but that is still returning an error. Here is the whole piece of code, the info being put in txt part would be letters and numbers, not sure if this makes a difference

VBA Code:
Private Sub Txtpart_AfterUpdate()
Dim Stock As Worksheet
Set Stock = Worksheets("Stock")
If WorksheetFunction.CountIf(Stock.Range("A:A"), Me.txtPart.Value) = 0 Then
MsgBox "Not in Stock"
Me.txtPart.Value = ""
Exit Sub
End If
With Me
.txtLoc = Application.WorksheetFunction.VLookup(CLng(Me.txtPart), Stock.Range("Lookup"), 2, 0)
End With
End Sub
 
Upvote 0
CLng(Me.txtPart)

Why are you converting the text to Long type if txtPart is letters and text?

What's the cell address of Stock.Range("Lookup") ?
 
Upvote 0
Thanks I think I have worked it out now, the issue was I hadnt got the named range lookup defined. What should i be using instead of CLng if its numbers and letters? thanks for your help,
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,457
Members
449,083
Latest member
Ava19

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