首页 文章

Excel,VBA在列中查找日期

提问于
浏览
0

我有这样的excel日历:
enter image description here

我想做什么 . 我想写一个宏,在标记单元格后按下按钮,宏获取字符串(此处为:user1),然后返回1列以获取时间并上去获取日期(在本例中为3) . 但我已经有了user1和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 - 每个日期(31,1,2 ......)它是一个合并在一起的2个单元格 . 如果user1单元格地址是f.e. 8,14所以3-11-2016细胞柱不是8但是7 .

有什么建议?

编辑:

似乎这个循环甚至不循环 . 当我将false更改为false时,msgbox没有出现 .

1 回答

  • 1

    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
    

相关问题