嵌套do whiles和嵌套ifs的VBA问题

时间:2015-05-22 16:06:46

标签: excel vba excel-vba

我有四列,使用嵌套的do while循环遍历两列,然后使用两个if语句作为约束。如果传递了两个if语句,则重新评估(粘贴也是一个选项),两个新单元格指向使用第一个循环上的索引检查的单元格的值,另外两个新单元格指向单元格的值使用嵌套循环上的索引进行检查。

代码:

Dim i
Dim j

i = 1
j = 1

Do Until IsEmpty(Range("BE" & i))
    Do Until IsEmpty(Range("BH" & j))
        If Cells(i, "BE").Value = Cells(j, "BH").Value Then
            If (Cells(j, "BG").Value - Cells(i, "BF")) < TimeValue("1:00:00") Then
            'This is not correctly filtering, dates/time are in
            ' mm/dd/yy hh:mm format

                Range("BJ" & i).Value = Range("BE" & i).Value
                Range("Bk" & i).Value = Range("BF" & i).Value
                Range("BL" & i).Value = Range("BG" & j).Value
                Range("BM" & i).Value = Range("BH" & j).Value
            End If
        End If

        j = j + 1
    Loop

    i = i + 1
    j = 1
  Loop

End Sub

它的作用:

几乎所有事情都正确。问题在于它没有正确地检查单元BG(j)和BF(i)之间的时间差是否<1。 60分钟。是否使用:

If (Cells(j, "BG").Value - Cells(i, "BF")) * 1440 < 60 Then

IF (Cells(j, "BG").Value - Cells(i, "BF")) < TimeValue("1:00:00") Then

差异为5小时的值被视为真实并通过if语句。

1 个答案:

答案 0 :(得分:4)

尝试在j = 1

之后添加i = i + 1