VBA EXCEL 多个嵌套 FOR 循环,为表达式设置两个变量

标签 vba loops excel for-loop nested

好吧,我做了很多搜索,找到了一些,玩了一点。我似乎无法让这些循环充分工作,我可以部分或另一部分,但不能全部。因为第一个循环工作正常,然后就变得不稳定了。

T 是表达式输出的目标t.Value = time1 - time2
Y 是设定的时间和日期,不会更改= time1
X 是时间和日期,必须从与相应 y 相同的列中的范围中提取。 x=时间2

我已上传工作簿的相应部分:

https://docs.google.com/open?id=0BzGnV1BGYQbvMERWU3VkdGFTQS1tYXpXcU1Mc3lmUQ

我玩过条件退出来重新排列 for 循环。我什至考虑过尝试 goto,直到我注意到它的提及所产生的一大堆尸体。

我愿意接受并感谢任何建议或指导。我注意到一些语言有退出和继续选项,但 VB 似乎没有?

这是我的循环,我已经去掉了我在尝试让它工作时造成的困惑。

Sub stituterangers()
Dim dify As Boolean
Dim difx As Boolean
Dim time2 As Date
Dim time1 As Date

For Each t In range("d7:cv7")
       For Each x In range("d8:cv11")
             If x > 0 Then time2 = x           
           For Each y In range("d2:cv2")
            time1 = y                     
        t.Value = time1 - time2
        t = 0
                Next y
      Next x
Next t
End Sub 


Sub stituterangersNEW()
Dim t As range
Dim x As range
Dim dify As Boolean
Dim difx As Boolean
Dim time2 As Date
Dim time1 As Date

On Error Resume Next

    'Looping through each of our output cells.
    For Each t In range("d7:cv7")
    
    
    
     For Each y In range("d2:cv2")
            If t.Column = y.Column Then
            time1 = y.Value
             If y = 0 Then Exit Sub
                End If
        
        For Each x In range("d8:cv11")
            'Check to see if our dep time corresponds to
            'the matching column in our output
            If t.Column = x.Column Then
                
                If x > 0 Then
                    time2 = x.Value
                    
                    t.Value = time1 - time2
                    
                    Exit For
                End If
            End If
            
            
            Next x
            
        Next y
    Next t

End Sub
  

最佳答案

我目前无法访问您的 Google 文档文件,但您的代码存在一些问题,我将在回答时尝试解决这些问题

Sub stituterangersNEW()
Dim t As Range
Dim x As Range
Dim dify As Boolean
Dim difx As Boolean
Dim time2 As Date
Dim time1 As Date

    'You said time1 doesn't change, so I left it in a singe cell.
    'If that is not correct, you will have to play with this some more.
    time1 = Range("A6").Value

    'Looping through each of our output cells.
    For Each t In Range("B7:E9") 'Change these to match your real ranges.

        'Looping through each departure date/time.
        '(Only one row in your example. This can be adjusted if needed.)
        For Each x In Range("B2:E2") 'Change these to match your real ranges.
            'Check to see if our dep time corresponds to
            'the matching column in our output
            If t.Column = x.Column Then
                'If it does, then check to see what our time value is
                If x > 0 Then
                    time2 = x.Value
                    'Apply the change to the output cell.
                    t.Value = time1 - time2
                    'Exit out of this loop and move to the next output cell.
                    Exit For
                End If
            End If
            'If the columns don't match, or the x value is not a time
            'then we'll move to the next dep time (x)
        Next x
    Next t

End Sub

编辑

我更改了您的工作表以供使用(请参阅上面的新子)。这可能不能直接满足您的需求,但希望它能展示我认为您想做的事情背后的概念。请记住,此代码并不遵循我建议的所有编码最佳实践(例如,验证时间实际上是一个 TIME,而不是一些随机的其他数据类型)。

     A                      B                   C                   D                  E
1    LOAD_NUMBER            1                   2                   3                  4
2    DEPARTURE_TIME_DATE    11/12/2011 19:30    11/12/2011 19:30    11/12/2011 19:30    11/12/2011 20:00                
4    Dry_Refrig 7585.1  0   10099.8 16700
6    1/4/2012 19:30

使用 sub 我得到了这个输出:

    A           B             C             D             E
7   Friday      1272:00:00    1272:00:00    1272:00:00    1271:30:00
8   Saturday    1272:00:00    1272:00:00    1272:00:00    1271:30:00
9   Thursday    1272:00:00    1272:00:00    1272:00:00    1271:30:00

关于VBA EXCEL 多个嵌套 FOR 循环,为表达式设置两个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9690484/

相关文章:

vba - 在 Excel 2013 而非 Excel 2010 中基于 Power Pivot 创建数据透视表

vba - 如何访问已打开的特定工作簿而不使用其名称?

java - for 循环中的 String.charAt(i) 在 Java 中不起作用

php - 从 MySQL 创建 Excel 文件

Excel VBA : Delete entire row if cell in column A is blank (Long Dataset)

vba - 如何将Word 2013中格式化的段落复制到Excel?

vba - 测试两个范围对象是否引用同一范围

javascript - 迭代 pug 中的 javascript 对象

c++ - 删除特定索引处的 vector 元素

excel - 将 CSV 导入 Excel - 自动 "Text to columns"和 "insert table"