Adding msgbox to code

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi,

Can anyone help, i am trying to get a msgbox to popup when no id is found.
I have a sheet called Info and in column G i have some names and in column H I have some id numbers.
I have a userformwith two textboxes when i scan an id into textbox7 that then searches the sheet
Info then populates textbox8 with the name in column G opposite that then puts textbox8 value into column H on the active
sheet which all works fine. What i would like is for a msgbox to popup if no id is found in column H
on the Info sheet. I am all new to this so any help would be great. Here is what I have at the minute.
Hope this makes sense.

Private Sub TextBox7_Change()
Dim i As Long, LastRow As Long
LastRow = Sheets("Info").Range("H" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Info").Cells(i, "H").Value = (Me.TextBox7) Or _
Sheets("Info").Cells(i, "H").Value = Val(Me.TextBox7) Then
Me.TextBox8 = Sheets("Info").Cells(i, "G").Value
End If
Next
End Sub

Private Sub TextBox8_Enter()
Application.DisplayAlerts = False
Dim LastRow As Long, ws As Worksheet
Set ws = ActiveSheet
LastRow = ws.Range("H" & Rows.Count).End(xlUp).Row + 1
ws.Range("H" & LastRow).Value = TextBox8.Text
TextBox7.Value = ""
TextBox8.Value = ""
Application.DisplayAlerts = True
Unload out2
out3.Show
End If
End Sub

Regards
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try
VBA Code:
Private Sub TextBox7_Change()
    Dim i As Long, LastRow As Long

    LastRow = Sheets("Info").Range("H" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Sheets("Info").Cells(i, "H").Value = (Me.TextBox7) Or _
            Sheets("Info").Cells(i, "H").Value = Val(Me.TextBox7) Then
            Me.TextBox8 = Sheets("Info").Cells(i, "G").Value
           
            Exit Sub 'add <<<<
           
        End If
    Next
   
    MsgBox "not found" ' add <<<<<<<<
End Sub
 
Upvote 0
"does not work" is a bit vague. More details about how it isn't working would help.

Also, by putting this in the Change event, you have a potential problem of the MsgBox comming up every key press of the entry into the textbox.
It might be better in the AfterUpdate event.
 
Upvote 0
Hi Mikerickson,

Sorry for the short reply, I have changed the change event to AfterUpdate and now it works thank you so much.

Regards
 
Upvote 0
Hi,

when the message box pops up when it has not found a match and I press ok it jumps straight to Private Sub TextBox8_Enter() how do I make it clear textbox 7 & 8 and start from Private Sub TextBox7_AfterUpdate() again.

Private Sub TextBox7_AfterUpdate()
Dim i As Long, LastRow As Long
LastRow = Sheets("Info").Range("H" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Info").Cells(i, "H").Value = (Me.TextBox7) Or _
Sheets("Info").Cells(i, "H").Value = Val(Me.TextBox7) Then
Me.TextBox8 = Sheets("Info").Cells(i, "G").Value
Exit Sub
End If
Next
MsgBox "not found"
End Sub

Private Sub TextBox8_Enter()
Application.DisplayAlerts = False
Dim LastRow As Long, ws As Worksheet
Set ws = ActiveSheet
LastRow = ws.Range("H" & Rows.Count).End(xlUp).Row + 1
ws.Range("H" & LastRow).Value = TextBox8.Text
TextBox7.Value = ""
TextBox8.Value = ""
Application.DisplayAlerts = True
Unload out2
out3.Show
End If
End Sub

Regards
 
Upvote 0
Hi,

It still jumps to Private Sub TextBox8_Enter() rather than starting from Private Sub TextBox7_AfterUpdate() again.

Regards
 
Upvote 0
Of course it goes to the Enter event.
You don't want it going to the AfterUpdate. The AfterUpdate checks to see if the user entered value is on the sheet. But there isn't any user entry yet.
You want the focus to go to the TextBox, so the user can enter the value that will later be checked by the AfterUpdate event.
And, by going to the TextBox, the Enter event fires.

To put the focus where you want it, you might try putting the data checking code in the BeforeUpdate event and, after the MsgBox, clear the two textboxes and set Cancel = True. That way, the focus never leaves the TextBox so the Enter event isn't triggered.
 
Upvote 0
Hi

Sorry for the late reply, I have done what you have suggested and it now works perfectly thank you so much.

Regards
 
Upvote 0

Forum statistics

Threads
1,224,875
Messages
6,181,516
Members
453,050
Latest member
Obil

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