Conditional Find

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I wish to search a selected range of cells containing dates for any desired
date. If the date I'm looking for is found, I want to run certain code. If
it's not found, then I'll run different code.

For example, if I'm searching for 3/15/08 and a match is found among the
selected range of cells, perform certain code. If this date is not found,
perform this other code. Thanks in advance for your help!
 
Hi Steve

You could try something like the code below that should give you an
idea of how to do what your after. I added this code to a user button
but it could be amended to fit what ever you like.

Option Explicit
Dim MyDate As Date 'declare your variable as a date

Private Sub CommandButton1_Click()

MyDate = InputBox("Please Enter a Date", "Date Finder") 'enter a date
to find

On Error Resume Next 'add this to trap the error on date not found

With Range("TestRng") 'set the range to search

.Find(What:=MyDate, LookAt:=xlWhole).Activate 'if found activate
the cell

If ActiveCell.Value = MyDate Then ' if active cell matches
your date do this

MsgBox "Run the code for a finding a date"

Else 'if it doesn't match then do this

MsgBox "Run the code for a date not found"

End If

End With

End Sub

Hope this helps

S
 
Check out the example in the VBA help. In the VBA editor, type Find,
then F1.

I think this is what you're looking for.
 

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

Back
Top