Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Phil Osman
 
Posts: n/a
Default Using Find in a macro

I have the following:
Cells.Select
Selection.Find(What:="1/07/2005", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("H95").Select

But, instead of the date that it is finding I want it to search for a date
that I previously copied in the macro. So literally:
Selection.Find(What:=Paste......

Can this be done somehow?

--
http://www.redbrick.dcu.ie/~pele
  #2   Report Post  
Rowan
 
Posts: n/a
Default

Use a variable to hold the date you want to find. Assign the value to the
variable at the same time that you do the copy and paste. So if the variable
is myDate you can use:

Selection.Find(What:=myDate,...

Also you might want to change the code to set a range variable to the found
cell which allows you to check that something was actually been found before
selecting it. This will prevent an error occuring if Excel doesn't find the
date you are searching for:

Dim myRange As Range
With Cells
Set myRange = .Find(myDate)
End With
If myRange Is Nothing Then
MsgBox "Not found"
Else
' do whatever to myRange eg select
myRange.Select
End If

Hope this helps
Rowan

"Phil Osman" wrote:

I have the following:
Cells.Select
Selection.Find(What:="1/07/2005", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("H95").Select

But, instead of the date that it is finding I want it to search for a date
that I previously copied in the macro. So literally:
Selection.Find(What:=Paste......

Can this be done somehow?

--
http://www.redbrick.dcu.ie/~pele

  #3   Report Post  
Phil Osman
 
Posts: n/a
Default

I don't understand the first bit. I named the cell that holds the Date as
"Month".
In my macro I then have:
Selection.Find(What:=Month,

but it doesn't work ?

--
http://www.redbrick.dcu.ie/~pele


"Rowan" wrote:

Use a variable to hold the date you want to find. Assign the value to the
variable at the same time that you do the copy and paste. So if the variable
is myDate you can use:

Selection.Find(What:=myDate,...

Also you might want to change the code to set a range variable to the found
cell which allows you to check that something was actually been found before
selecting it. This will prevent an error occuring if Excel doesn't find the
date you are searching for:

Dim myRange As Range
With Cells
Set myRange = .Find(myDate)
End With
If myRange Is Nothing Then
MsgBox "Not found"
Else
' do whatever to myRange eg select
myRange.Select
End If

Hope this helps
Rowan

"Phil Osman" wrote:

I have the following:
Cells.Select
Selection.Find(What:="1/07/2005", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("H95").Select

But, instead of the date that it is finding I want it to search for a date
that I previously copied in the macro. So literally:
Selection.Find(What:=Paste......

Can this be done somehow?

--
http://www.redbrick.dcu.ie/~pele

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a macro to find duplicate names Carter Devereaux Excel Discussion (Misc queries) 1 July 6th 05 10:39 PM
Macro - message box if find nothing Frantic Excel-er Excel Discussion (Misc queries) 8 July 1st 05 08:45 PM
Can't find macro in an Excel file to delete it Ron Excel Discussion (Misc queries) 3 July 1st 05 01:07 PM
Macro / Compile Error / Duplicate Declaration carl Excel Worksheet Functions 1 June 29th 05 08:55 PM
Date macro Hiking Excel Discussion (Misc queries) 9 February 3rd 05 12:40 AM


All times are GMT +1. The time now is 12:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"