我有一个升序日期列表。我使用以下公式返回单元格引用:
=CELL("address",INDEX(A2:A14,MATCH(D1,A2:A14,1)))
这个公式给了我结果,例如搜索 1/2/2017 返回 $A$6 的单元格引用。 而搜索 12/30/2016 返回单元格引用 $A$4。
如果搜索日期大于列表中的上一个日期并且存在另一个大于搜索日期的日期,我将如何使用 VBA 实现相同的结果,同时返回列表中的下一个日期值?
Dates 1/2/2017
12/27/2016 $A$6
12/28/2016
12/29/2016
1/1/2017
1/2/2017
1/3/2017
...
如果我回答了你的问题,我有一个替代方案给你。 我已经测试了这段代码并且工作正常。
我有一个从 A2 到 A14 的升序日期列表 我使用单元格(1,4([工作表中是D1]作为输入数据以进行比较到列表中。 比较的结果是进入单元格(2,4([工作表中为D2]
即 我从 A2 到 A14 的列表
12/27/2016
12/28/2016
12/29/2016
01/01/2017
01/02/2017
01/03/2017
01/04/2017
01/05/2017
01/06/2017
01/07/2017
01/08/2017
01/09/2017
01/10/2017
进入单元格(1,4( 我写了 01/05/2017
输出宏进入单元格(2,4(,为 :$A$9
如果我写 01/11/2017 结果是 $A$14
Sub test()
Dim i, start, finish As Integer
Dim myDate,output As Date
i = 0
start = 2
finish = 14
myDate = Cells(1, 4) ' my input - cells(2,4) is output
For i = finish To start Step -1
If (i = start) Then
Cells(i, 1).Activate 'cell where is "my date"
Cells(2, 4) = ActiveCell.Address ' get the address -output
'Exit For
End If
If myDate >= Cells(i, 1) Then
Cells(i, 1).Activate 'cell where is "my date"
Cells(2, 4) = ActiveCell.Address ' get the address -output
Exit For
End If
Next i
End Sub