Can't figure out what's wrong with the code.
I have a code where I need to check if in certain sheet can be found certain products' ID before making an order.
Private Sub Pardot_Click()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind As String
valueToFind = pardID
Set xlSheet = ActiveWorkbook.Worksheets("Noliktava")
Set xlRange = xlSheet.Range("A1:A500")
For Each xlCell In xlRange
If xlCell.Value <> valueToFind Then
MsgBox ("This product wasn't found in the database - ID: " & pardID.Text)
Exit Sub
End If
Next xlCell
End Sub
Basically, I launch the userform and type in the ID (ie 1) in the box and click "Okay" or whatever, if the ID can't be found in the range (ID:1) I want it to show the error Msg.
Code works if I change <> to =, but that's not the needed result.
Without changing your code too much, and probably saving some time, instead of checking each cell in the range, just use CountIf()?
Private Sub Pardot_Click()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind As String
valueToFind = pardID
Set xlSheet = ActiveWorkbook.Worksheets("Noliktava")
Set xlRange = xlSheet.Range("A1:A500")
If WorksheetFunction.CountIf(xlRange,valuetoFind) = 0 then
msgbox "This product wasn't found in the database - ID: " & parId.textEnd
End If
End Sub
Note: this will look for exactly the text the user inserted. Use wildcards if it can be somewhere in a string (ie search for "dog" in "doggone", "dog food","dog")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.