Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Excel] Using the Find method 1

Status
Not open for further replies.

Skullmonkey

Computer
Dec 10, 2001
12
0
0
US
I hate to ask these kind of questions, but... why doesn't this work? I'm getting a compile error on the
Code:
Set dtFindMe
line.

Code:
Sub FindDate()
  Dim dtFindMe As Date
  Dim rgFoundMe As Range
  
  Set dtFindMe = DateSerial(2002, 2, 22)
  Set rgFoundMe = Range("A:A").Find(what:=dtFindMe)
  
  If rgFoundMe Is Nothing Then
    Exit Sub
  End If
  
  rgFoundMe.Font.Bold = True
  
End Sub

Any help would be appreciated.
SM
 
Replies continue below

Recommended for you

DateSerial returns a Variant and you only need to use the Set statement for objects, not variables.
Code:
  Dim dtFindMe As Date
  
  dtFindMe = CDate(DateSerial(2002, 2, 22))
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Does, then, the following line not need its
Code:
Set
? Or is a range not an object?

Code:
Set rgFoundMe = Range("A:A").Find(what:=dtFindMe)

SM
 
Yes, the Find method returns a Range object, so it would require the set statement. DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
The code works in this variant:

Dim dtFindMe As Date
Dim rgFoundMe As Range

dtFindMe = DateSerial(2002, 2, 22)

Set rgFoundMe = Range("A:A").Find(what:=dtFindMe)

If rgFoundMe Is Nothing Then
Exit Sub
End If

rgFoundMe.Font.Bold = True

End Sub

dtFindMe it is not an object and you do not have to use "Set". !!
 
Status
Not open for further replies.
Back
Top