2017-08-30 50 views
0

我想編輯一些VBA代碼,當前循環直到某個測試被滿足。我想要做的是循環,直到滿足兩個條件。我很難做到這一點。這是原始代碼,它的工作原理。在VBA DO中測試多個標準雖然循環

Loop 
    For j = 1 To 29 
    Increment = Worksheets("Price & Energy Curve").Cells(8 + Start + j, 3).Value 
    l = Int((Worksheets("Capacity").Cells(57 + j, 12).Value) * (-1)/Increment) 
    k = WorksheetFunction.Max((l + 1) * Increment, 0) 
    Worksheets("Capacity").Activate 
    ActiveSheet.Cells(56 + j, 17).Value = k 
    test = ActiveSheet.Cells(57 + j, 13).Value 
    Do While test <> True 
    k = k + Increment 
    ActiveSheet.Cells(56 + j, 17).Value = k 
    test = ActiveSheet.Cells(57 + j, 13).Value 
    Loop 
    Next j 

我想補充的

test = ActiveSheet.Cells(57 + j, 37).Value 

因此額外的測試,當

test = ActiveSheet.Cells(57 + j, 13).Value 

test = ActiveSheet.Cells(57 + j, 37).Value 

爲真,則退出循環。

+0

你寫的答案在你的問題'如果測試= ActiveSheet.Cells(57 + J,13).value的和測試= ActiveSheet.Cells(57 + J,37) .Value然後退出 – jsotola

回答

0

檢查這個循環

Do While test1 <> True Or test2 <> True 
k = k + Increment 
ActiveSheet.Cells(56 + j, 17).Value = k 
test1 = ActiveSheet.Cells(57 + j, 13).Value 
test2 = ActiveSheet.Cells(57 + j, 37).Value 
Loop