How to make match() work with date in excel vba?

后端 未结 6 762
萌比男神i
萌比男神i 2021-01-13 04:16

I\'m having problem making the match() work in excel VBA. The code is:

x = Application.Match(\"Sep 2008\", Range(\"F1:F1\"), 0)

The value i

6条回答
  •  没有蜡笔的小新
    2021-01-13 04:43

    I think I can safely assume that the value in F1 is a date. In you code "Sep 2008" is a string. You will never be able to get a successful match as long as your datatypes are inconsistent. If you are looking for a date, then make sure that the first parameter is a date.

    Dim dSearchSDate As Date
    
    dSearchSDate = "01/Sept/2008"
    
    x = Application.Match(dSearchSDate, Range("F1:F1"), 0)
    

    Here is another possible approach.

    Sub temp()
    Dim x
    Dim dSearchSDate As Date
    
        dSearchSDate = "01/Sept/2008"
    
        If ThisWorkbook.Worksheets(1).Range("F1:F1").Value = dSearchSDate Then
            Debug.Print "Found it!"
        Else
            Debug.Print "Doh!!"
        End If
    
    End Sub
    

提交回复
热议问题