Matching user input to number in range

W

WLMPilot

I have a userform used to input a supply order using item number. The stock
items are listed on worksheet("Items"). I do not want to use a combo or
listbox. I want the user to enter the item number and then have a macro
check the range (A3:A, using xldown to determine actual range) and see if the
item number entered is a valid number before advancing to quantity field.

I thought about reading the data into an array and looping each time to
check but figure there is a quicker and easier way.

Thank,
Les
 
D

David

Hi,

try something like this
Sub Macro1()
Range("A3").Select
X = InputBox("Input", "Title")

Range(Selection, Selection.End(xlDown)).Select
Selection.Find(What:=(X), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
End Sub
 
J

JLGWhiz

Hi Les, I agree with David that the Find method is probably the fastest.
You can set up a validation loop with a message box in case the data entered
by the user is not found, so that it will recycle to the input box for the
user to make correction to their original entry or to just go to the next
item. That will speed up the process, rather than have the procedure
terminate because there was no match.
 
W

WLMPilot

That was my next question. How do I trap if value not found? I have a
couple of books I used to understand the FIND function. The user will input
a three digit number (as a string). I need to match cell content, which will
hold a three digit number formatted to text. I figure the WHAT, SEARCHORDER,
and SEARCHDIRECTION is all I need, but I do not know how to trap if value not
found.

Les
 
J

JLGWhiz

This is a generic Find statement with a validation
loop built in. It assumes there would only be one
occurence of the ItemToFind, if any. You can
modify the find statement with more arguments and
also include an option to exit the Sub instead of
looping. You can also modify it to do a Find Next.

Sub TrapNoFind()
Dim c As Range, ItemToFind As Variant
Retry:
ItemToFind = InputBox("Enter Item to Find", "Title")
Set c = Worksheets(1).Range("A2:A50") _
.Find(ItemToFind, LookIn:=xlValues)
If Not c Is Nothing Then
'Do something
Else
GoTo Retry:
End If
End Sub
 
W

WLMPilot

Thanks for your help. Just what I needed.

Les

David said:
Hi,

try something like this
Sub Macro1()
Range("A3").Select
X = InputBox("Input", "Title")

Range(Selection, Selection.End(xlDown)).Select
Selection.Find(What:=(X), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
End Sub
 
W

WLMPilot

Thanks for your help. I googled the FIND function and after adapting to what
I needed and finding out how to trap with the IF statement, got it to work
for the most part. Still working out clitches.

I would like an explanation on the IF statement you indicate. Just reading
it makes no sense to me, ie IF Not c Is Nothing. The way I figured out what
this meant was reaching back into math class and saying two negatives (ie IF
NOT...IS NOTHING) make a positive, ie if entry is valid (item number found),
then do something ELSE reenter item number.

How exactly is this code deteremined?

Thanks,
Les
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Verifying input 1
SetFocus not working 4
Verifying Input 7
Blank item in listbox, how to remove 3
Listbox vs Combobox 3
Populating a combobox 1
Range Selection 17
Verify Input 2

Top