'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
- Change the LookIn parameter to xlFormulas.
- Widen the column until the macro works with LookIn:=xlValues.
- Reduce the font-size until the macro works with LookIn:=xlValues.
Steps to reproduce:
- Insert a date into A2 (eg. 7/3).
- Merge Across 4 columns (A2:D2) - this is the field for the date to be found
- Create a set of sequential dates in cells A4:A35 (eg. 1/3 to 31/3).
- 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