Help with VBA code

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
581
Office Version
  1. 2013
Platform
  1. Windows
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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,450
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
581
Office Version
  1. 2013
Platform
  1. Windows
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,450
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
581
Office Version
  1. 2013
Platform
  1. Windows

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
 

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
581
Office Version
  1. 2013
Platform
  1. Windows
Thanks VoG much appreciated
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,200
Messages
5,835,948
Members
430,396
Latest member
dzifna

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