2013-07-02 98 views
0

我想複製下面的代碼與數組,以避免重複代碼6次。 代碼顏色的兩個基於一個值,在這種情況下TueCum和TuePer盒子 - 我也有WedCum,WedPer等PowerPoint VBA - For循環和陣列

If Val(TueCum) >= Val(ProdTarg) * ((2 * Sixth) + SixthLim) Then 
    TueCum.BackColor = &HFF00& 
    TuePer.BackColor = &HFF00& 
    ElseIf Val(TueCum) < Val(ProdTarg) * ((2 * Sixth) - SixthLim) Then 
    TueCum.BackColor = &HFF& 
    TuePer.BackColor = &HFF& 
    Else 
    TueCum.BackColor = &H80FF& 
    TuePer.BackColor = &H80FF& 
End If 

我已經遠遠該得到的,

Dim Cum(0 To 5) 
Dim CDay As Variant 
Count = 1 

Set Cum(0) = MonCum 
Set Cum(1) = TueCum 
Set Cum(2) = WedCum 
Set Cum(3) = ThuCum 
Set Cum(4) = FriCum 
Set Cum(5) = SatCum 

For Each CDay In Cum 
    If Val(CDay) >= Val(ProdTarg) * ((Count * Sixth) + SixthLim) Then 
     CDay.BackColor = &HFF00& 
     TuePer.BackColor = &HFF00& 
     ElseIf Val(CDay) < Val(ProdTarg) * ((Count * Sixth) - SixthLim) Then 
     CDay.BackColor = &HFF& 
     TuePer.BackColor = &HFF& 
     Else 
     CDay.BackColor = &H80FF& 
     TuePer.BackColor = &H80FF& 
    End If 
Count = Count + 1 
Next CDay 

但我似乎無法讓它同時格式化兩個文本框,我試着有第二個數組,但不能正確的。 所以我需要使其顏色TuePer和TueCum當計算TuePer,顏色WedPer和WedCum當計算WedPer等

回答

2

這應該工作:

Dim Cum(1 To 6) 
Dim Per(1 To 6) 
Dim Count as Integer 

Set Cum(1) = MonCum 
Set Per(1) = MonPer 
Set Cum(2) = TueCum 
Set Per(2) = TuePer 
Set Cum(3) = WedCum 
Set Per(3) = WedPer 
Set Cum(4) = ThuCum 
Set Per(4) = ThuPer 
Set Cum(5) = FriCum 
Set Per(5) = FriPer 
Set Cum(6) = SatCum 
Set Per(6) = SatPer 

For Count = 1 To 6 
    If Val(Cum(Count)) >= Val(ProdTarg) * ((Count * Sixth) + SixthLim) Then 
     Cum(Count).BackColor = &HFF00& 
     Per(Count).BackColor = &HFF00& 
    ElseIf Val(Cum(Count)) < Val(ProdTarg) * ((Count * Sixth) - SixthLim) Then 
     Cum(Count).BackColor = &HFF& 
     Per(Count).BackColor = &HFF& 
    Else 
     Cum(Count).BackColor = &H80FF& 
     Per(Count).BackColor = &H80FF& 
    End If 
Next Count 
+0

喜@ d-stroyer,謝謝! 這很棒,我必須做的一點點改變,就是'If Val(Cum(Count))> = Val(ProdTarg)' 再次感謝您的幫助! – Conquistador

+0

+1不錯的答案。 – brettdj