comparing textbox value with values in named range

J

jeff

I have textbox1 that will hold a date value entered by user.
I have a named range "PayWeek5" that shows a series of dates.

I want to do an If-Then that compares the date entered by user to the
dates in the range "PayWeek5".

If the date from the testbox matches any one of the dates in the
range, then.....

Here's a simple example of what I need. I've looked in this site, but
could not see anything like what I'm looking for.

If TextBox1.Value = Range "PayWeek5".Value then 'matching any of the
dates in the range.
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = TextBox2.Value 'enters value in textbox2

Thanks
jeff
 
D

Dave Peterson

You could loop through each of the cells in the range.

dim FoundAMatch as boolean
dim myCell as range
foundamatch = false
for each mycell in worksheets("Sheet99").range("payweek5").cells
if mycell.value = cdate(me.textbox1.value)
foundamatch = true
exit for
end if
next mycell

if foundamatch then
...

=========
If the range is a single area, you could try:

if application.countif(worksheets("sheet99").range("payweek5"), _
cdate(me.textbox1.value)) then

'there's a match.

========
Remember to validate that the value in the textbox is an actual date, too!
 
J

jeff

You could loop through each of the cells in the range.

dim FoundAMatch as boolean
dim myCell as range
foundamatch = false
for each mycell in worksheets("Sheet99").range("payweek5").cells
    if mycell.value = cdate(me.textbox1.value)
       foundamatch = true
       exit for
    end if
next mycell

if foundamatch then
   ...

=========
If the range is a single area, you could try:

if application.countif(worksheets("sheet99").range("payweek5"), _
                        cdate(me.textbox1.value))then

     'there's a match.

========
Remember to validate that the value in the textbox is an actual date, too!

Thanks Dave. I don't understand the coding, but as with others, I'll
study it and figure it out along the way. In the mean time, this
works. I appreciate it very much. Thanks for taking the time.
jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top