Help with VBA code

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
551
I cannot understand why the first line of code works fine but the second does not all I have changed is the 2 to a date.

Set c = Sheet1.Range("b2:ae2").Find(2, LookIn:=xlValues)


Set c = Sheet1.Range("b2:ae2").Find("09/08/2010", LookIn:=xlValues)

Thanks you for yo0ur help
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,378
Try something like this...
Code:
Set c = Sheet1.Range("b2:ae2").Find([COLOR="Red"]DateValue("09/08/2010")[/COLOR], LookIn:=xlValues)

Your date was just a Text string in quotes and that is not the same as an actual date in a cell. The DATEVALUE function will convert your date text string into an actual date.
 

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
551
Thanks AlphaFrog its worked, but I has hoping that the .find method would tell me the location of the clolumn that the date was found in not the actual date.
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,378
Code:
Sub Macro1()

    Dim c As Long, cLtr As String
    
    c = Sheet1.Range("b2:ae2").Find(DateValue("09/08/2010"), LookIn:=xlValues).Column
    
    If c > 0 Then
    
        cLtr = Replace(Mid(Columns(c).Address, 2, 2), ":", "")
        
        MsgBox "Date found in..." & vbCr & vbCr & _
               "Column number " & c & vbCr & _
               "Column letter " & cLtr
    Else
        MsgBox "No match found."
    End If

End Sub
 

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
551

ADVERTISEMENT

Hi AlphaFrog
I get the error below on this line of code (sorry I am new to VBA)

c = Sheet1.Range("b2:ae2").Find(DateValue("09/08/2010"), LookIn:=xlValues).Column

Run-time error 91
object variable or with block variable not set
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try this

Code:
Sub Macro1()
Dim Found As Range
Set Found = Sheet1.Range("b2:ae2").Find(what:=DateValue("09/08/2010"), LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "No match found.", vbExclamation
Else
    MsgBox "Match found in " & Found.Address(False, False), vbInformation
End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,491
Messages
5,596,477
Members
414,070
Latest member
DuncanLucas

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top