Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-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
Joined
Dec 10, 2001
Messages
12
Location
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
 
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.

Part and Inventory Search

Sponsor

Back
Top