0

我正在尝试使用数组复制以下代码,以避免重复代码 6 次。代码根据 1 的值为两个框着色,在本例中为 TueCum 和 TuePer - 我还有 WedCum、WedPer 等。

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 时为 TuePer 和 TueCum 着色,在计算 WedPer 时为 WedPer 和 WedCum 着色等。

4

1 回答 1

2

This should work:

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
于 2013-07-02T12:30:58.233 回答