2012-03-13 132 views
2

确定两个的变量,所以我已经做了搜索发现了一些和扮演和小一个很好的协议。我似乎无法让这些循环充分发挥作用,我可以得到一部分或另一部分,但不是全部。正如第一个循环工作正常,那么它会变得诡异。VBA EXCEL多重嵌套for循环是设置表达

T是用于表达输出t.Value = time1 - time2
Y目的地是一个设定的时间和日期不改变= time1
X是时间和日期,并且必须从范围中提取在相同的列作为对应yx= time 2

我已经上传我的工作簿

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

我与条件退出重新安排用于循环起到相应的部分。我甚至考虑过试试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 

回答

1

我不能让你的谷歌文档文件的时刻,但也有一些问题,你的代码,我会尽量解决在回答

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 

编辑

我改变了你工作表一起玩(见上新亚)。这可能不会直接满足您的需求,但希望它能展示我认为您想要做的背后的任务。请记住,这段代码并不遵循我所推荐的所有编码最佳准则(例如验证时间实际上是一个时间,而不是一些随机的其他数据类型)。

 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 

使用我得到这个输出子:

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 
+0

谢谢你的回复。这有助于我了解条件退出如何与我的想法一起工作,也许我的做法是不正确的。有了这个以及我无法让循​​环同步的代码。 我将使用列D作为示例在列d中,我有 1个单元输出,将= t 1个单元格用于y与时间 X由4个单元格d8,d9,d10,d11组成, d列中循环的一个值。 – PCGIZMO 2012-03-14 12:41:27

+0

实质上我需要...设置对应的y值在列d,通过4个细胞搜索x的值,一旦T被计算的I需要挑选了整个事情了其移动到下一列目前列计算T. d通过简历。我的方法是否正确?我在想,用这种方法和X扫描列,然后行而不是行,然后下一列也将是一个问题。自98,99以来,我没有做很多的编程工作,那是qbasic。所以我很兴奋 – PCGIZMO 2012-03-14 12:41:47

+0

@PCGIZMO'啊,我从QB开始回来的时候。 :-)'你可以在你的问题中发布你的数据和你期望从中得到的输出的例子吗?我认为这会对我有所帮助,因为我仍然不能100%确定我知道你想要得到什么。你是否试图将d-cells的值设置为相同的值(y的值)? – Gaffi 2012-03-14 13:30:50