ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Conditional Find (https://www.excelbanter.com/excel-programming/389613-conditional-find.html)

Steve C

Conditional Find
 
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!
--
Steve C

Incidental

Conditional Find
 
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




David Sisson[_3_]

Conditional Find
 
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.


Steve C

Conditional Find
 
Thanks! That works nicely.
--
Steve C


"Incidental" wrote:

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






All times are GMT +1. The time now is 06:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com