Slight alteration to existing code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I am using the working code supplied below.
At present some part numbers had started with a zero but now some do & dont use the zero in the part number.
If i type say 12345 nothing is found BUT if i type 012345 i then see the required item.
To save me chaning a lot of part numbers can the code be altered & then check for either.


VBA Code:
Private Sub CheckIfNumberExists_Click()
   Dim ans As String
   Dim Fnd As Range
   Do Until Not Fnd Is Nothing
      ans = InputBox("Enter search for value")
      If ans = "" Then Exit Sub
      Set Fnd = Range("A:A").Find(ans, , , xlWhole, , , False, , False)
      If Fnd Is Nothing Then
         If MsgBox("The Part Number " & ans & " Is Not In The List" & vbLf & vbLf & "Do You Want To Search Again ?", vbYesNo + vbCritical, "PART NUMEBR NOT FOUND") = vbNo Then Exit Sub
      Else
         If MsgBox("The Part Number  " & ans & "  Can Be Found In Row  " & Fnd.Row & vbLf & vbLf & "Search For Another ?", vbYesNo + vbExclamation, "PART NUMBER WAS FOUND") = vbYes Then Set Fnd = Nothing Else Exit Sub
      End If
   Loop
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I have not tested these solutions.

You can use xlPart instead of xlWhole. Then if you type 12345 it will match 012345. The problem is that it will also match 123456 and 912345.

You can use wildcards:
Rich (BB code):
Set Fnd = Range("A:A").Find("*" & ans, , , xlWhole, , , False, , False)
Then if you type 12345 it will match 012345. The problem is that it will also match 912345.

You can check twice:
Rich (BB code):
      Set Fnd = Range("A:A").Find(ans, , , xlWhole, , , False, , False)
      If Fnd Is Nothing Then
         Set Fnd = Range("A:A").Find("0" & ans, , , xlWhole, , , False, , False)
         If Fnd Is Nothing Then
            If MsgBox("The Part Number " & ans & " Is Not In The List" & vbLf & vbLf & "Do You Want To Search Again ?", vbYesNo + vbCritical, "PART NUMEBR NOT FOUND") = vbNo Then Exit Sub
         Else
            If MsgBox("The Part Number  " & ans & "  Can Be Found In Row  " & Fnd.Row & vbLf & vbLf & "Search For Another ?", vbYesNo + vbExclamation, "PART NUMBER WAS FOUND") = vbYes Then Set
         End If
      Else
         If MsgBox("The Part Number  " & ans & "  Can Be Found In Row  " & Fnd.Row & vbLf & vbLf & "Search For Another ?", vbYesNo + vbExclamation, "PART NUMBER WAS FOUND") = vbYes Then Set Fnd = Nothing Else Exit Sub
      End If

This adds more code but will find only 12345 or 012345.
 
Upvote 0
I did try part but with the current code in use it would only advise on the first item row number that was a match.
I dont need it to be more complicated.
 
Upvote 0
If your part numbers are 6 digit then you could force user to enter full number in your input box

Untested but maybe update to your code will do what you want

VBA Code:
Private Sub CheckIfNumberExists_Click()
   Dim ans As String
   Dim Fnd As Range
   Dim msg As VbMsgBoxResult
   Do
    ans = InputBox("Enter Six Digit Part No.", "Search")
'cancel pressed
      If StrPtr(ans) = 0 Then Exit Sub
    If ans Like "######" Then
      Set Fnd = Range("A:A").Find(Val(ans), , , xlWhole, , , False, , False)
      If Fnd Is Nothing Then
         msg = MsgBox("The Part Number " & ans & " Is Not In The List" & vbLf & vbLf & _
                    "Do You Want To Search Again ?", vbYesNo + vbCritical, "PART NUMEBR NOT FOUND")
                    If msg = vbNo Then Exit Sub
      Else
         msg = MsgBox("The Part Number  " & ans & "  Can Be Found In Row  " & Fnd.Row & vbLf & vbLf & _
         "Search For Another ?", vbYesNo + vbExclamation, "PART NUMBER WAS FOUND")
         If msg = vbNo Then Exit Sub
      End If
    End If
    Set Fnd = Nothing
   Loop
End Sub

Dave
 
Upvote 0
No,
I enter 029022 but it advises me it isnt found.

Thanks for trying,ive an hour spare so im going to start and delete all zero's
 
Upvote 0
No,
I enter 029022 but it advises me it isnt found.

That may be due to my adding the Val function

Rich (BB code):
Set Fnd = Range("A:A").Find(Val(ans), , , xlWhole, , , False, , False)

Could try removing it & see if helps

VBA Code:
Set Fnd = Range("A:A").Find(ans, , , xlWhole, , , False, , False)

Dave
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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