Macro unique number lookup - exact match required

davidf120

New Member
Joined
Jan 20, 2018
Messages
10
Hi All,

I am using the following code:

Sub ReturnInvoice()
Do


Dim myWord$
myWord = InputBox("Insert the Voucher Number", "Voucher Search")
If myWord = "" Then Exit Sub


Application.ScreenUpdating = False
Dim xRow&, NextRow&, LastRow&
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
NextRow = Sheets("Returned").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
For xRow = 2 To LastRow
If InStr(Cells(xRow, 1).Value, myWord) > 0 Then
Rows(xRow).Cut Sheets("Returned").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
Application.ScreenUpdating = True
Sheets("Returned").Range("J" & Rows.Count).End(xlUp).Offset(1).Value = Now
Sheets("Invoices Out").Select
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
Loop


End Sub

Which I sourced I think on here for someone else's issue. However I have encountered a bit of an issue with it.

Basically the macro pops up a lookup box, in which we put in a unique voucher number, it finds that number and then copies the entire row into another sheet.

Issue is if for example I search for voucher number "5", the macro is finding every cell with a 5 in it and returning all those rows, eg 15, 25 55 etc. I only want it to return the exact number entered.

Any ideas? I tried playing with lookat whole but didn't seem to work.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This line ...

Code:
If InStr(Cells(xRow, 1).Value, myWord) > 0 Then

... is looking for any cells that contain the search criteria. You could try changing it to ...

Code:
If Cells(xRow, 1).Value = myWord Then

WBD
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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