Help with VBA code

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
586
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,220
Members
448,554
Latest member
Gleisner2

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
Back
Top