2
votes

'Found an interesting one - after 4 hours of tearing my hair out.

It seems that the Excel 2010 VBA won't find a date value in a range of merged-across cells if the first column's width is too narrow for the font size used. (This is similar to Excel VBA being unable to find a date value in a hidden row/column).

3 Possible Solutions: best first

  1. Change the LookIn parameter to xlFormulas.
  2. Widen the column until the macro works with LookIn:=xlValues.
  3. Reduce the font-size until the macro works with LookIn:=xlValues.

Steps to reproduce:

  1. Insert a date into A2 (eg. 7/3).
  2. Merge Across 4 columns (A2:D2) - this is the field for the date to be found
  3. Create a set of sequential dates in cells A4:A35 (eg. 1/3 to 31/3).
  4. Merge Across 4 columns (A4:D35)

Run the following code:

Sub findDate()
Dim myRange As Range
Dim myDate As Date
Dim myFindDate As Date
Dim myRow As Integer

With ActiveSheet

    Set myRange = .[A2]

    myFindDate = .[A4:D35].Value

    On Error Resume Next

    myRow = myRange.Find( _
        what:=myFindDate, _
        LookIn:=xlValues, _
        LookAt:=xlWhole, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=False, _
        SearchFormat:=False).Row

    On Error GoTo 0

    If myRow <> 0 Then
        MsgBox "The date is in row number = " & myRow
    Else
        MsgBox "Column A too narrow.  Either use LookIn:=xlFormulas, widen Column A or reduce the font size."
    End If

End With

End Sub

Notice that the message box presents the relevant row number.

Now reduce the width of Column A to 2.4 and run the code again.

Notice the message box produced: Excel VBA is no longer able to find the date!

Here's the code for solution 1, above:

Sub findDate()
Dim myRange As Range
Dim myDate As Date
Dim myFindDate As Date
Dim myRow As Integer

With ActiveSheet

    Set myRange = .[A2]

    myFindDate = .[A4:D35].Value

    On Error Resume Next

    myRow = myRange.Find( _
        what:=myFindDate, _
        LookIn:=xlFormulas, _
        LookAt:=xlWhole, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=False, _
        SearchFormat:=False).Row

    On Error GoTo 0

    If myRow <> 0 Then
        MsgBox "The date is in row number = " & myRow
    Else
        MsgBox "Column A too narrow.  Either use LookIn:=xlFormulas, widen Column A or reduce the font size."
    End If

End With

End Sub

(The only change is in the LookIn parameter: xlFormulas instead of xlValues)

If you run this second bit of code, the message box will present the row number again.

'Hope this saves someone else the pain it caused me!!

Gary

1

1 Answers

0
votes

I followed your "Steps to reproduce" instructions and your example would not work for me.

Some things I have noticed though.

Dim myDate As Date
Dim myFindDate As Date
Dim myRow As Integer

The values may be dates but you are working with ranges. So start the code correctly,

 Dim myRange As Range, myFindDate As Range, myRow As Range

then set the ranges correctly.

 Set myRange = [A2]
 Set myFindDate = [A4:D35]
 Set myRow = myFindDate.Find(what:=myRange, lookat:=xlWhole)

Using the code this way, it does not matter how wide the columns are.

Complete code.

Sub findDateB()
    Dim myRange As Range, myFindDate As Range, myRow As Range

    Set myRange = [A2]
    Set myFindDate = [A4:D35]
    Set myRow = myFindDate.Find(what:=myRange, lookat:=xlWhole)

    If Not myRow Is Nothing Then
        MsgBox "The date is in row number = " & myRow.Row
    Else: MsgBox "Not Found"
        Exit Sub
    End If

End Sub