another simple thing probably

chris9104

Board Regular
Joined
Feb 7, 2009
Messages
55
Hi guys, the below code should search my range for an existing value a return a msgbox if it does

'Check Record Does Not Already Exist
If Application.CountIf(Range("Staff Numbers"), txtstaffno.Value) > 0 Then
MsgBox "Staff Number Already Exists.", vbExclamation, "Error"
Me.txtstaffno.SetFocus
Exit Sub
End If

However I just cant get it to work, please help
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Two things i would look at are...

in this part,
If Application.CountIf(Range("Staff Numbers"), txtstaffno.Value) > 0 Then
this bit txtstaffno.Value should probably have Me. in front of it.
like,
If Application.CountIf(Range("Staff Numbers"), Me.txtstaffno.Value) > 0 Then

Also

in this part,
If Application.CountIf(Range("Staff Numbers"), txtstaffno.Value) > 0 Then
this bit Range("Staff Numbers") should probably have a sheet name in front of it.
like,
If Application.CountIf(Sheets("Sheet1").Range("Staff Numbers"), Me.txtstaffno.Value) > 0 Then

Other than that i can see no reason why it would not work.
 
Last edited:
Upvote 0
Yeah I did end up using the same method as above so ended up with:

'Check Record Does Not Already Exist
If Application.CountIf(Sheets("Staff Data").Range("StaffNumbers"), Me.txtstaffno.Value) > 0 Then
MsgBox "Staff Number Already Exists.", vbExclamation, "Error"
Me.txtstaffno.SetFocus
Exit Sub
End If

then also realised could do it this way:

'Check Record Does Not Already Exist<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
If WorksheetFunction.CountIf(Worksheets("Staff Data").Range("StaffNumbers"),Me.txtstaffno.Value) > 0 Then<o:p></o:p>
MsgBox "Staff Number Already Exists.", vbExclamation, "Error"<o:p></o:p>
Me.txtstaffno.SetFocus<o:p></o:p>
Exit Sub<o:p></o:p>
End If

Thanks for the replies
 
Upvote 0
Glad you got it working!
thumbup.gif
 
Upvote 0

Forum statistics

Threads
1,203,269
Messages
6,054,471
Members
444,727
Latest member
Mayank Sharma

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