vbOK, IF statement met

dversloot1

Board Regular
Joined
Apr 3, 2013
Messages
113
Hello,

I'm using a user form to allow users to enter a new Product ID for a New Product Form. I want to stop the user from using a Product ID that is already in use. The code seems to stop any number inputted into the NewID input box.
Any idea why?

Private Sub CreateNewQMA_Click()
Dim Question As String


Question = "ID already exists. Please choose another."


If IsError(Application.Match(NewID.Value, Sheets("Data").Range("B1:B2000"), 0)) Then
If MsgBox(Question, vbOK, "Double Checking") = vbOK Then
NewID.Value = ""
End If


Exit Sub


Else


End If


ClearFields
Range("C3") = NewID.Value
Unload LoadForm
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This looks a bit backwards to me... It looks like if you do NOT find the match (which would throw the error you catch with If IsError), you say ID already exists (which it does not because there was no match) and exit sub. If you find the match, you then clear fields and make c3 = the id...

Am I reading this wrong, or are you backwards here?
 
Upvote 0
This returns TRUE if it does not already exist.
If IsError(Application.Match(NewID.Value, Sheets("Data").Range("B1:B2000"), 0)) Then

I think you want this...
If Not IsError(Application.Match(NewID.Value, Sheets("Data").Range("B1:B2000"), 0)) Then
 
Last edited:
Upvote 0
It wasn't noticing the difference between IF and IF NOT. I made some alterations to it using the FIND functions rather than the MATCH function and came up with this:

Private Sub CreateNewQMA_Click()
Dim Question As String
Dim foundrng As Range


Set findrng = Sheets("Data").Range("B1:b2000")
Set foundrng = findrng.Find(NewID.Value)


Question = "ID already exists. Please choose another."


If Not (foundrng Is Nothing) Then
MsgBox (Question)


Exit Sub


Else


End If


ClearFields
Range("C3") = NewID.Value
Unload LoadForm
End Sub

Worked this way.
 
Upvote 0
Match probably didn't work because NewID.Value is a text string and Range("B1:B2000") are numeric values.
 
Upvote 0

Forum statistics

Threads
1,203,696
Messages
6,056,764
Members
444,891
Latest member
MelissaBr

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