2012-03-13 67 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