Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Problem with using variable in Find statement

I am writing a macro to locate the current date (in form mm/dd/yy) from a
column of dates in column A1. I am using a variable that is set to the value
of cell G1 that uses the NOW function to return the current date. However,
the Find method does not seem to like a variable. Below is the code I am
currently using to locate the current date. When I run this I get an error,
please help!

Dim LookFor
LookFor = Range("G1").Select
Range("A1").Select
Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:=_
False).Activate
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Problem with using variable in Find statement

try replacing

LookFor = Range("G1").Select

with

LookFor = Range("G1")


Lee-Z



"DOOGIE" wrote in message
...
I am writing a macro to locate the current date (in form mm/dd/yy) from a
column of dates in column A1. I am using a variable that is set to the
value
of cell G1 that uses the NOW function to return the current date. However,
the Find method does not seem to like a variable. Below is the code I am
currently using to locate the current date. When I run this I get an
error,
please help!

Dim LookFor
LookFor = Range("G1").Select
Range("A1").Select
Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:=_
False).Activate



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Problem with using variable in Find statement

Now returns a date and time. If you values in column A are just Dates, your
value will not be found.

Dim LookFor as Long
Dim rng as Range
LookFor = clng(Range("G1"))
set rng = Columns(1).Find(What:=LookFor, After:=Range("A1"), _
LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
if not rng is nothing then
rng.Select
else
msgbox format(Lookfor,"mm/dd/yy") & " was not found"
End if

--
Regards,
Tom Ogilvy


"DOOGIE" wrote in message
...
I am writing a macro to locate the current date (in form mm/dd/yy) from a
column of dates in column A1. I am using a variable that is set to the

value
of cell G1 that uses the NOW function to return the current date. However,
the Find method does not seem to like a variable. Below is the code I am
currently using to locate the current date. When I run this I get an

error,
please help!

Dim LookFor
LookFor = Range("G1").Select
Range("A1").Select
Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:=_
False).Activate



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Problem with using variable in Find statement

That did not work. I still receive the error "Object variable not set". Thank
you for the help, though.

"Lee-Z" wrote:

try replacing

LookFor = Range("G1").Select

with

LookFor = Range("G1")


Lee-Z



"DOOGIE" wrote in message
...
I am writing a macro to locate the current date (in form mm/dd/yy) from a
column of dates in column A1. I am using a variable that is set to the
value
of cell G1 that uses the NOW function to return the current date. However,
the Find method does not seem to like a variable. Below is the code I am
currently using to locate the current date. When I run this I get an
error,
please help!

Dim LookFor
LookFor = Range("G1").Select
Range("A1").Select
Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:=_
False).Activate




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Problem with using variable in Find statement

That did the trick! Thank you for the help!

"Tom Ogilvy" wrote:

Now returns a date and time. If you values in column A are just Dates, your
value will not be found.

Dim LookFor as Long
Dim rng as Range
LookFor = clng(Range("G1"))
set rng = Columns(1).Find(What:=LookFor, After:=Range("A1"), _
LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
if not rng is nothing then
rng.Select
else
msgbox format(Lookfor,"mm/dd/yy") & " was not found"
End if

--
Regards,
Tom Ogilvy


"DOOGIE" wrote in message
...
I am writing a macro to locate the current date (in form mm/dd/yy) from a
column of dates in column A1. I am using a variable that is set to the

value
of cell G1 that uses the NOW function to return the current date. However,
the Find method does not seem to like a variable. Below is the code I am
currently using to locate the current date. When I run this I get an

error,
please help!

Dim LookFor
LookFor = Range("G1").Select
Range("A1").Select
Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:=_
False).Activate






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problem with using variable in Find statement


read what Tom Ogilvy wrote that should fix your proble

--
reddog906
-----------------------------------------------------------------------
reddog9069's Profile: http://www.excelforum.com/member.php...fo&userid=2445
View this thread: http://www.excelforum.com/showthread.php?threadid=38989

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
Variable in if statement Francis Hookham Excel Worksheet Functions 9 December 12th 07 02:35 PM
if than statement has too many variable repke New Users to Excel 1 May 2nd 06 02:00 PM
Macro Creating Variable and using variable in a SQL statement Jimmy Excel Programming 4 October 25th 04 02:36 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM
Problem trying to us a range variable as an array variable TBA[_2_] Excel Programming 4 September 27th 03 02:56 PM


All times are GMT +1. The time now is 04:15 AM.

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

About Us

"It's about Microsoft Excel"