首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA EXCEL为表达式设置两个变量的多个嵌套FOR循环

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

Stack Overflow用户
提问于 2012-03-14 03:11:55
回答 1查看 106.1K关注 0票数 2

好的,我已经做了大量的搜索,找到了一些,玩了很少。我似乎不能让这些循环完全工作,我可以在部分或另一个上,但不是整个。正如第一个循环工作正常,然后它变得摇摇欲坠。

T是表达式输出t.Value = time1 - time2的目标

Y是一个不会更改= time1的设置时间和日期

X是时间和日期,必须从与相应y相同的列的范围中提取。x= time 2

我已经上传了我的工作簿的相应片段

代码语言:javascript
复制
https://docs.google.com/open?id=0BzGnV1BGYQbvMERWU3VkdGFTQS1tYXpXcU1Mc3lmUQ

我尝试过重新安排for循环的条件退出。我甚至考虑过尝试goto,直到我注意到它的提及造成了一大堆身体。

我乐于接受任何建议或指导,并对此表示感谢。我注意到有几种语言有退出和继续选项,但VB没有吗?

这是我的循环,我在试着让它工作时,把我弄得乱七八糟的东西去掉了。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-14 03:43:51

我现在无法访问您的google docs文件,但您的代码有一些问题,我将在回答时尝试解决这些问题

代码语言:javascript
复制
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

编辑

我更改了您的工作表以进行操作(有关新的Sub,请参阅上文)。这可能不能直接满足您的需求,但希望它能演示我认为您想要做的事情背后的概念。请记住,此代码并不遵循我推荐的所有编码最佳精度(例如,验证时间实际上是一个时间,而不是某个随机的其他数据类型)。

代码语言:javascript
复制
     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,我得到了以下输出:

代码语言:javascript
复制
    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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9690484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档