IF values not recognizing numbers

Skrej

Board Regular
Joined
May 31, 2013
Messages
176
Office Version
  1. 365
Platform
  1. Windows
I have the following code snippet which is supposed to check the values of some textboxes and dropdown boxes on a userform, in order to avoid duplicate entries.

Code:
'check for duplicates
rng = Sheet2.Cells(Rows.Count, "E").End(xlUp).Row
For Each MyCell In Sheet2.Range("E7:E" & rng)
If MyCell = Me.Reg3.Value And MyCell.Offset(0, 1).Value = Me.Reg4.Value And MyCell.Offset(0, 2).Value = Me.Reg5.Value Then
MsgBox "This training already exists for this staff member"
Exit Sub
End If

While doing some testing, I couldn't figure out why it wasn't picking up duplicate entries. Then, I discovered that if that those values in the E column were alphanumeric, the code worked as expected, and gave the warning about a duplicate entry, and exited the sub as expected. However, if the values were strictly numeric, I don't get the message, and the rest of the sub continues to run and enters a duplicate entry.

Ultimately, I'll need those values (which are employee ID #'s) to be only numbers, so any suggestions on how to tweak my code? I can't change the format of the employee ID numbers.

Why aren't those IF statements recognizing duplicate numbers?

Edit: I realized it might help to show what I'd declared my variables as, so here they are.

Code:
Dim cNum As Integer
Dim nextrow As Range
Dim MyCell As Range
Dim rng As Long
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try
Code:
If MyCell = cdbl(Me.Reg3.Value)
or
Code:
If MyCell = clng(Me.Reg3.Value)
 
Last edited:
Upvote 0
What is MyCell referring to?

In the 2 line you state
For Each MyCell In Sheet2.Range("E7:E" & Rng)

Meaning MyCell is a Range

but in line 4 you forget to call the .Value of it on the first AND

MyCell = Me.Reg3.Value

Code:
'check for duplicates
rng = Sheet2.Cells(Rows.Count, "E").End(xlUp).Row
[COLOR=#ff0000][B]For Each MyCell In Sheet2.Range("E7:E" & rng)[/B][/COLOR]
If [COLOR=#ff0000][B]MyCell = Me.Reg3.Value[/B][/COLOR] And MyCell.Offset(0, 1).Value = Me.Reg4.Value And MyCell.Offset(0, 2).Value = Me.Reg5.Value Then
MsgBox "This training already exists for this staff member"
Exit Sub
End If
 
Upvote 0
Try
Code:
If MyCell = cdbl(Me.Reg3.Value)
or
Code:
If MyCell = clng(Me.Reg3.Value)


Thanks, I tried the 2nd one, which did the trick. however, I'm still not sure what the problem was with recognizing numbers versus mixed numbers and letters.
 
Upvote 0
What is MyCell referring to?

In the 2 line you state
For Each MyCell In Sheet2.Range("E7:E" & Rng)

Meaning MyCell is a Range

but in line 4 you forget to call the .Value of it on the first AND

MyCell = Me.Reg3.Value

Code:
'check for duplicates
rng = Sheet2.Cells(Rows.Count, "E").End(xlUp).Row
[COLOR=#ff0000][B]For Each MyCell In Sheet2.Range("E7:E" & rng)[/B][/COLOR]
If [COLOR=#ff0000][B]MyCell = Me.Reg3.Value[/B][/COLOR] And MyCell.Offset(0, 1).Value = Me.Reg4.Value And MyCell.Offset(0, 2).Value = Me.Reg5.Value Then
MsgBox "This training already exists for this staff member"
Exit Sub
End If

Yes, MyCell is checking individual cells in a dynamic range from E7 to the last entry in Col E. If I understand you correctly, you're saying line four should read:
Code:
If [B]MyCell.[COLOR=#ff0000]Value[/COLOR] = Me.Reg3.Value[/B] And MyCell.Offset(0, 1).Value = Me.Reg4.Value And MyCell.Offset(0, 2).Value = Me.Reg5.Value Then

I did try adding in that .Value, but it didn't change the results.
 
Upvote 0
All values in a text box are text (ie as string), so if you have a number you need to convert it from a string back to a number
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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