Excel,VBA在列中查找date

我有Excel这样的日历: 在这里输入图像说明

我正在尝试做什么。 我想写一个macros,在标记单元格并按下button后,Macro获取string(这里是:user1),然后返回1列获取时间,并获取date(在这种情况下为3)。 但是我已经得到了user1和date,但我不能得到date

这是一个代码

Dim cell As Object Dim client As String Dim date As Date Dim hour As Date Dim rowc As Long Dim colc As Long For Each cell In Selection client = cell.Value colc = ActiveCell.Column rowc = ActiveCell.Row Next cell hour = Cells(rowc, colc - 1).Value Cells(4, 17).Value = hour 'works to this point t = rowc colc = colc-1 For i = t To 0 If IsDate(Cells(i, colc).Value) = True Then date= Cells(i, colc).Value Else rowc = rowc - 1 End If Next i 'Cells(3, 17).Value = date Cells(5, 17).Value = client 

行的解释:colc = colc-1 – 每个date(31,1,2 …)是合并在一起的2个单元格。 如果user1的单元格地址是fe 8,14,那么3-11-2016单元格列不是8而是7。

有什么build议么?

编辑:

看来这个循环甚至不会循环。 当我更改true为false,msgbox没有出现。

For循环不能这样工作。 如果t = 10,并且你有For i = t To 0 ,那么循环将不会迭代一次,因为10> 0。就我所了解的你的要求而言,你想使i变得更好。 尝试使用Do While循环时也是如此:

 i = t Do while i > 0 If IsDate(Cells(i, colc).Value) = True Then date= Cells(i, colc).Value Else rowc = rowc - 1 End If i = i - 1 Loop