Search for employee stamp number

dandy

New Member
Joined
Apr 3, 2017
Messages
22
I have the module below that searches for employees by their stamp number (i.e. W-2, W-21, W-25, etc.)

What it's doing incorrectly is when I search for "W-2", it brings up all the employees with a "2" in the stamp (e.g. I search for W-2, it brings ups W-2, W-21, W-22, W-23, etc.)

Is there a way to adjust this code so that it searches uniquely for the value that i enter?

Code:
'search by stamp function
Sub StampSearch()
    Call InitVars
    StampTab.Cells.ClearContents
    StampTab.Cells.ClearFormats
    Dim Found As Integer
    'this looks to see if it is a numeric stamp or an alphabetic stamp (HYD, etc)
    'it is also the main search function.  It will return multiple values
    'if the stamp appears more than once
    Found = 0
    i = 2
    For x = 4 To LastRow
        If IsNumeric(Stamp) Then
            EmployeeStampNum = Stamp
            If InStr(1, MatrixTab.Cells(x, 4), EmployeeStampNum) Then
                NameLocation = x
                StampTab.Cells(i, 1) = EmployeeStampNum
                StampTab.Cells(i, 2) = MatrixTab.Cells(x, 1)
                StampTab.Cells(i, 3) = MatrixTab.Cells(x, 2)
                i = i + 1
                Found = 1
            Else
            End If
        Else
            EmployeeStampString = Stamp
            If InStr(1, MatrixTab.Cells(x, 5), EmployeeStampString) Then
                NameLocation = x
                StampTab.Cells(i, 1) = EmployeeStampString
                StampTab.Cells(i, 2) = MatrixTab.Cells(x, 1)
                StampTab.Cells(i, 3) = MatrixTab.Cells(x, 2)
                i = i + 1
                Found = 1
            Else
            End If
        End If
    Next x
    'This is error proofing that allows the macro to avoid searching
    'for an empty stamp number
    If Stamp = "0" Or Stamp = "" Then
        Found = 0
    End If
    
    If Found = 0 Then
        MsgBox ("You did not enter a valid stamp number.  Try using CAPITALS and removing all spaces.")
    Else
        StampTab.Cells(1, 1) = "Stamp #"
        StampTab.Cells(1, 2) = "Employee Name"
        StampTab.Cells(1, 3) = "Current Employee"

Thanks,
Dandy
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
It is difficult to follow your code because it has many undeclared variables. Can you post a screen shot of what your data looks like? Section B at this link has instructions on how to post a screen shot: https://www.mrexcel.com/forum/board-announcements/127080-guidelines-forum-use.html Alternately, you could upload a copy of your file to a free site such as www.box.com. or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Include a detailed explanation of what you would like to do referring to specific cells and worksheets. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Sorry, it's giving me issues with inserting the screen shot.

Basically, the gist of it is that I'd like the "EmployeeStampNum" to be a unique value instead of returning anything that contains W-2

Code:
        If IsNumeric(Stamp) Then
            EmployeeStampNum = Stamp
            If InStr(1, MatrixTab.Cells(x, 4), EmployeeStampNum) Then
                NameLocation = x
                StampTab.Cells(i, 1) = EmployeeStampNum
                StampTab.Cells(i, 2) = MatrixTab.Cells(x, 1)
                StampTab.Cells(i, 3) = MatrixTab.Cells(x, 2)
                i = i + 1
                Found = 1
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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