Show error if no match then clear textbox

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hello All,

I have a userform with 2 texboxes where I scan a barcode in both and if they match the textboxes turn green and red if no match, I am trying to show an error message if there if no match
then clear both textboxes but I can not figure out how to do it as I am very new to VBA, could anyone help.
Below is what I have at the minute.

Private sub TextBox2_Change ()
If TextBox5.Text = TextBox2.Text then
TextBox2.Backcolor = RGB(51, 255, 51)
TextBox5.Backcolor = RGB(51, 255, 51)
Else
TextBox2.Backcolor = RGB(255, 0, 0)
TextBox5.Backcolor = RGB(255, 0, 0)
End If
End Sub

Regards

 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this

Code:
Private Sub TextBox2_Change()
    Call matchText
End Sub


Private Sub TextBox5_Change()
    Call matchText
End Sub


Sub matchText()
    If TextBox2.Value = "" Or TextBox5.Value = "" Then Exit Sub
    If TextBox5.Text = TextBox2.Text Then
        TextBox2.BackColor = RGB(51, 255, 51)
        TextBox5.BackColor = RGB(51, 255, 51)
    Else
        TextBox2.BackColor = RGB(255, 0, 0)
        TextBox5.BackColor = RGB(255, 0, 0)
        MsgBox "no match"
        TextBox2.Value = ""
        TextBox5.Value = ""
        TextBox2.BackColor = RGB(255, 255, 255)
        TextBox5.BackColor = RGB(255, 255, 255)
    End If
End Sub
 
Upvote 0
Hi DanteAmor,

Thank you for the code but I can not get it work, when I scan textbox5 to match textbox2 the error message pops up then disappears and both boxes are cleared and none of them turn green for a match or red for a fail.

Regards
 
Upvote 0
Hi DanteAmor,

Thank you for the code but I can not get it work, when I scan textbox5 to match textbox2 the error message pops up then disappears and both boxes are cleared and none of them turn green for a match or red for a fail.

Regards


Hello, in which part of your initial requirement do you want to put the data on the sheet?

[h=2]Show error if no match then clear textbox[/h]
Hello All,

I have a userform with 2 texboxes where I scan a barcode in both and if they match the textboxes turn green and red if no match, I am trying to show an error message if there if no match
then clear both textboxes but I can not figure out how to do it as I am very new to VBA, could anyone help.
Below is what I have at the minute.​


To be clear, you could explain step by step what you want to do.
1. I capture data in the textbox2.
What should happen?
2. I capture data in the textbox5.
What should happen?
3. I press the button commandbutton1.
What must happen
Forget the code. All explained with clear examples.
 
Upvote 0
Hi DanteAmor,

Thank you for the code but I can not get it work, when I scan textbox5 to match textbox2 the error message pops up then disappears and both boxes are cleared and none of them turn green for a match or red for a fail.

Regards

forgive the sheet. capture the 2 texbox, press the button, if they are the same they turn green. if they are different they turn red and a message appears. press ok, textboxes are cleaned.
 
Upvote 0
Hi DanteAmor,

Thank you for the code but I can not get it work, when I scan textbox5 to match textbox2 the error message pops up then disappears and both boxes are cleared and none of them turn green for a match or red for a fail.

Regards

Forgive de button, i dont know what happend to me this morning, :banghead:
 
Upvote 0
Hope this makes sense.
1:I have a combobox where I select a product which then populates information in to textbox2, 3 and 4 I then scan in to textbox5 which needs to do a match on textbox2
2:If there is a match change textbox2 & 5 background colour to green if no match to red then show an error message and when I press ok on the error message it clears textbox2 & 5 text for them to try again.
I am really sorry if this is not making any sense.

Regards
 
Upvote 0
Hope this makes sense.
1:I have a combobox where I select a product which then populates information in to textbox2, 3 and 4 I then scan in to textbox5 which needs to do a match on textbox2
2:If there is a match change textbox2 & 5 background colour to green if no match to red then show an error message and when I press ok on the error message it clears textbox2 & 5 text for them to try again.
I am really sorry if this is not making any sense.

Regards


Replace the code with the following one, please try and tell me.

Code:
Private Sub TextBox5_Change()    Call matchText
End Sub




Sub matchText()
    If TextBox2.Value = "" Or TextBox5.Value = "" Then Exit Sub
    If TextBox5.Value = TextBox2.Value Then
        TextBox2.BackColor = RGB(51, 255, 51)
        TextBox5.BackColor = RGB(51, 255, 51)
    Else
        TextBox2.BackColor = RGB(255, 0, 0)
        TextBox5.BackColor = RGB(255, 0, 0)
        MsgBox "no match"
        TextBox2.Value = ""
        TextBox5.Value = ""
        'TextBox2.BackColor = RGB(255, 255, 255)
        'TextBox5.BackColor = RGB(255, 255, 255)
        TextBox2.Setfocus 
    End If
End Sub
 
Upvote 0
Still no joy when i scan in to textbox5 the text appears and thats it no background colour or error message on fail. Does all the code need to be in the userform or some in a module?
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

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