Return msgbox if value not found - VBA

manny88

New Member
Joined
Oct 28, 2016
Messages
33
Hi I am using a search function on a userform and want the ID entered in the textbox to search for everything in Column B. If the value is found then it will display in the other textboxes - this bit works for me.

What doesn't is if the value is Not found then show msgbox to say "value not found".

VBA Code:
Private sub searchbutton_click()

Dim ID_ref as string
ID_ref = trim(search box.text)
Lastrow =worksheets("Admin").cells(Rows.count, 2).End(club).Row

If Trim(ID_ref) ="" Then
   Magbox"search field is blank"
   End if
   Exit sub

[B]If trim(worksheets("admin").cells(i, 2) <> Trim(ID_ref) Then
   Msgbox "item not found"
   End if
   Exit sub[/B]

If trim(worksheets("admin").cells(i, 2) = Trim(ID_ref) Then
Type.text = worksheets("admin").cells(i,4).value
Exit for
End if
Next
End sub

Bit in bold is what's not working. When I run the sub first condition works but then it doesn't look for the value i enter and nor does it return those that exist.

Any help with this will be greatly appreciated
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I haven't gone into your code in any depth, but this line stood out to me, in particular what I highlighted in red... shouldn't that be xlUp instead?
Rich (BB code):
Lastrow =worksheets("Admin").cells(Rows.count, 2).End(club).Row
 
Upvote 0
I haven't gone into your code in any depth, but this line stood out to me, in particular what I highlighted in red... shouldn't that be xlUp instead?
Rich (BB code):
Lastrow =worksheets("Admin").cells(Rows.count, 2).End(club).Row

Hi thanks for the reply. Yes, apologies that's a typo. I had to write it out on the phone as the document is on a work computer. To confirm, I have it as xlUp
 
Upvote 0
I think you are going to have to wait till you get home and copy/paste your actual code. I copy/pasted what you posted and 5 lines of code lit up as having syntax errors.
 
Upvote 0
Code should read as

VBA Code:
Private sub searchbutton_click()

Dim ID_ref as string
ID_ref = trim(search box.text)
Lastrow =worksheets("Admin").cells(Rows.count, 2).End(xlUp).Row

If Trim(ID_ref) ="" Then
   Magbox"search field is blank"
   End if
   Exit sub

[B]If trim(worksheets("admin").cells(i, 2) <> Trim(ID_ref) Then
   Msgbox "item not found"
   End if
   Exit sub[/B]

If trim(worksheets("admin").cells(i, 2) = Trim(ID_ref) Then
Type.text = worksheets("admin").cells(i,4).value
Next
End if
End sub
 
Upvote 0
I think you are going to have to wait till you get home and copy/paste your actual code. I copy/pasted what you posted and 5 lines of code lit up as having syntax errors.
So if you take out the is empty and does not equal code it works to the point where it searches the ID that is being searched for and the desired column(s)
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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