Add msgbox if value not found

srkmfc

New Member
Joined
Aug 23, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,
Could you please help to add a msgbox if value entered in a text box is not found?
Thanks in advance.

Private Sub CommandButton1_Click()
Dim fila, contador As Integer

fila = 2
contador = 0

While Sheets("data2").Cells(fila, 1) <> Empty And contador = 0

If Sheets("data2").Cells(fila, 1) = TextBox2 Then

Label25 = Cells(fila, 3)
Label26 = Cells(fila, 5)
Label27 = Cells(fila, 6)
Label28 = Cells(fila, 8)
Label29 = Cells(fila, 10)
Label30 = Cells(fila, 11)
Label31 = Cells(fila, 13)
Label46 = Cells(fila, 15)
Label47 = Cells(fila, 16)
Label34 = Cells(fila, 18)
Label35 = Cells(fila, 20)
Label36 = Cells(fila, 21)
Label37 = Cells(fila, 23)
Label38 = Cells(fila, 25)
Label39 = Cells(fila, 26)
Label40 = Cells(fila, 28)
Label41 = Cells(fila, 30)
Label42 = Cells(fila, 31)
Label43 = Cells(fila, 33)
Label44 = Cells(fila, 35)
Label45 = Cells(fila, 36)

contador = 1

Else

fila = fila + 1
contador = 0

End If
Wend
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
VBA Code:
Private Sub CommandButton1_Click()
Dim fila, contador As Integer

fila = 2
contador = 0

While Sheets("data2").Cells(fila, 1) <> Empty And contador = 0

If Sheets("data2").Cells(fila, 1) = TextBox2 Then

Label25 = Cells(fila, 3)
Label26 = Cells(fila, 5)
Label27 = Cells(fila, 6)
Label28 = Cells(fila, 8)
Label29 = Cells(fila, 10)
Label30 = Cells(fila, 11)
Label31 = Cells(fila, 13)
Label46 = Cells(fila, 15)
Label47 = Cells(fila, 16)
Label34 = Cells(fila, 18)
Label35 = Cells(fila, 20)
Label36 = Cells(fila, 21)
Label37 = Cells(fila, 23)
Label38 = Cells(fila, 25)
Label39 = Cells(fila, 26)
Label40 = Cells(fila, 28)
Label41 = Cells(fila, 30)
Label42 = Cells(fila, 31)
Label43 = Cells(fila, 33)
Label44 = Cells(fila, 35)
Label45 = Cells(fila, 36)

contador = 1

Else

fila = fila + 1
contador = 0
msgbox "Entry '" & Sheets("data2").Cells(fila, 1) & "' not found.",vbinformation
End If
Wend
End Sub
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim fila, contador As Integer

fila = 2
contador = 0

While Sheets("data2").Cells(fila, 1) <> Empty And contador = 0

If Sheets("data2").Cells(fila, 1) = TextBox2 Then

Label25 = Cells(fila, 3)
Label26 = Cells(fila, 5)
Label27 = Cells(fila, 6)
Label28 = Cells(fila, 8)
Label29 = Cells(fila, 10)
Label30 = Cells(fila, 11)
Label31 = Cells(fila, 13)
Label46 = Cells(fila, 15)
Label47 = Cells(fila, 16)
Label34 = Cells(fila, 18)
Label35 = Cells(fila, 20)
Label36 = Cells(fila, 21)
Label37 = Cells(fila, 23)
Label38 = Cells(fila, 25)
Label39 = Cells(fila, 26)
Label40 = Cells(fila, 28)
Label41 = Cells(fila, 30)
Label42 = Cells(fila, 31)
Label43 = Cells(fila, 33)
Label44 = Cells(fila, 35)
Label45 = Cells(fila, 36)

contador = 1

Else

fila = fila + 1
contador = 0
msgbox "Entry '" & Sheets("data2").Cells(fila, 1) & "' not found.",vbinformation
End If
Wend
End Sub
Thanks for your message, but it keeps telling me that the text I submit is not found even if it's in the data2.

I need a code that pop-ups the msgbox whenever a value is not found in data2.
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim fila, contador As Integer
Dim TxtFnd as Boolean

fila = 2
contador = 0
TxtFnd = False

While Sheets("data2").Cells(fila, 1) <> Empty And contador = 0

If Sheets("data2").Cells(fila, 1) = TextBox2 Then
TxtFnd = True
Label25 = Cells(fila, 3)
Label26 = Cells(fila, 5)
Label27 = Cells(fila, 6)
Label28 = Cells(fila, 8)
Label29 = Cells(fila, 10)
Label30 = Cells(fila, 11)
Label31 = Cells(fila, 13)
Label46 = Cells(fila, 15)
Label47 = Cells(fila, 16)
Label34 = Cells(fila, 18)
Label35 = Cells(fila, 20)
Label36 = Cells(fila, 21)
Label37 = Cells(fila, 23)
Label38 = Cells(fila, 25)
Label39 = Cells(fila, 26)
Label40 = Cells(fila, 28)
Label41 = Cells(fila, 30)
Label42 = Cells(fila, 31)
Label43 = Cells(fila, 33)
Label44 = Cells(fila, 35)
Label45 = Cells(fila, 36)

contador = 1

Else

fila = fila + 1
contador = 0
End If
Wend

If TxtFnd = False then
msgbox "No Entry found.",vbinformation
End If
End Sub
 
Upvote 0
Solution
VBA Code:
Private Sub CommandButton1_Click()
Dim fila, contador As Integer
Dim TxtFnd as Boolean

fila = 2
contador = 0
TxtFnd = False

While Sheets("data2").Cells(fila, 1) <> Empty And contador = 0

If Sheets("data2").Cells(fila, 1) = TextBox2 Then
TxtFnd = True
Label25 = Cells(fila, 3)
Label26 = Cells(fila, 5)
Label27 = Cells(fila, 6)
Label28 = Cells(fila, 8)
Label29 = Cells(fila, 10)
Label30 = Cells(fila, 11)
Label31 = Cells(fila, 13)
Label46 = Cells(fila, 15)
Label47 = Cells(fila, 16)
Label34 = Cells(fila, 18)
Label35 = Cells(fila, 20)
Label36 = Cells(fila, 21)
Label37 = Cells(fila, 23)
Label38 = Cells(fila, 25)
Label39 = Cells(fila, 26)
Label40 = Cells(fila, 28)
Label41 = Cells(fila, 30)
Label42 = Cells(fila, 31)
Label43 = Cells(fila, 33)
Label44 = Cells(fila, 35)
Label45 = Cells(fila, 36)

contador = 1

Else

fila = fila + 1
contador = 0
End If
Wend

If TxtFnd = False then
msgbox "No Entry found.",vbinformation
End If
End Sub
Now it works, thank you so much!!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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