Siddharth
Siddharth

Reputation: 45

Values are not matching

I am newbie in VBA. Here is the question, I'm getting a value from Text box and matching it with a value in the excel sheet. Even though both the values are same, it's showing false. Here is the code.

Private Sub CommandButton4_Click()
    Dim retdata As Variant
    Dim empid1 As Variant

    retdata = TextBox1.Text
    empid1 = Sheets("Sheet2").Cells(retdata + 1, 1)

    If empid1 = retdata Then
        Sheets("Sheet2").Rows(retdata + 1).Copy Destination:=Sheets("Sheet1").Range("A16")
    Else
        MsgBox ("Not Found")
    End If
End Sub

Upvotes: 0

Views: 62

Answers (1)

Sam
Sam

Reputation: 5731

Here are a few things to try:
First, make sure that you are comparing strings with strings. Variants can hold a lot of stuff. Change the if statement to this

If CStr(empid1) = CStr(retdata) Then

If that doesn't help, then add these lines just before the if statement

Debug.Print "*" & retdata & "*"
Debug.Print "*" & empid1 & "*"

Bring up the debug window with Ctrl+G and examine the output.

Upvotes: 1

Related Questions