VBA问题与嵌套do whiles和嵌套ifs
我有四列,使用嵌套的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)之间的时间差是否小于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语句。
尝试在i = i + 1
之后添加j = 1
i = i + 1