Excel 如何在VBA中为未格式化为日期的日期行使用Range.find或Match函数?

Excel 如何在VBA中为未格式化为日期的日期行使用Range.find或Match函数?,excel,vba,Excel,Vba,我需要在与用户输入日期匹配的范围内找到列值。 我有一个范围,其中一行有故意不格式化为日期的月份日期 我尝试使用.find方法和application.match,但这两种方法都不适用于我(即使保证用户输入的日期在范围内) 以下是代码的相关部分: Dim vCurrentDate As Variant Dim iCurrentDateCol As Integer Dim rRng As Range 'Sheet8.Range("s1") is formatted as yyyy/mm/dd vC

我需要在与用户输入日期匹配的范围内找到列值。

我有一个范围,其中一行有故意不格式化为日期的月份日期

我尝试使用.find方法和application.match,但这两种方法都不适用于我(即使保证用户输入的日期在范围内)

以下是代码的相关部分:

Dim vCurrentDate As Variant
Dim iCurrentDateCol As Integer
Dim rRng As Range

'Sheet8.Range("s1") is formatted as yyyy/mm/dd
vCurrentDate = Sheet8.Range("s1")

'HolidaySchedule is for C$5:$AJ$90
'The row with dates has dates formatted as "Monday", "Tuesday", etc"
Set rRng = Sheet3.Range("HolidaySchedule")
尝试1:使用Application.Match(类似地尝试使用Worksheetfunction) 结果:需要运行时错误“424”对象

尝试2:使用.Find


它应该是这样工作的:

Dim SomeValue As String: SomeValue = ""
Dim Adress As Range
Dim Int As Variant

Set Adress = Sheets(1).Range("A:A").Find(SomeValue)
Int = Adress.Value

vCurrentDate可能仍然是一个问题。

在评论的帮助下,以下是我自己的问题的答案,如果有人好奇,请回答我自己的问题:

iTestCurrentDateCol = Application.Match(CDbl(vCurrentDate), rRng, 0)

尝试2肯定不起作用,因为
Find
返回的是范围对象,而不是整数(无论如何都要使用Long)。您能提供一些示例数据吗?我刚刚尝试了你的格式组合,效果很好。另外,您是否在工作表中尝试了一个简单的匹配函数(不使用VBA)以查看问题所在?@SJR-如果我将iTestCurrentDateCol设置为范围,然后找到iTestCurrentDateCol的值,它会起作用吗?@M.Schalk:在工作表中使用匹配函数对我来说是正确的(返回:21)假设vCurrentDate=18/09/2019假设F6:AJ6的文本值为星期日、星期一、星期二等…对应于2019年9月1日、2019年9月2日、2019年9月3日等,就是这样做的,检查Find是否返回某个值,如果是,则使用范围的列属性。日期可能很棘手,如感谢代码!我这样做了,但仍然不适用于我,即使我使用set address=rRng.Find(CDbl(vCurrentDate),lookin:=xlvalues)如果address为Nothing,那么MsgBox“Nothing”其他Int=address.value MsgBox Int End如果MsgBox显示为“Nothing”,即使我使用.Match找到了日期,仍然有效
Dim SomeValue As String: SomeValue = ""
Dim Adress As Range
Dim Int As Variant

Set Adress = Sheets(1).Range("A:A").Find(SomeValue)
Int = Adress.Value
iTestCurrentDateCol = Application.Match(CDbl(vCurrentDate), rRng, 0)