質問

I'm trying to create an interactive PowerPoint (isn't Excel because it needs to be displayed). I'm trying to do some conditional formatting with code. The ProdArray are all textboxes that I want to colour based on their value compared to another textbox (ProdTarg)

Dim ProdArray(0 To 5) As String
Dim Day As Variant
ProdArray(0) = MonProd
ProdArray(1) = TueProd
ProdArray(2) = WedProd
ProdArray(3) = ThuProd
ProdArray(4) = FriProd
ProdArray(5) = SatProd

For Each Day In ProdArray
    If Val(Day) >= Val(ProdTarg) * (Sixth + SixthLim) Then
        Day.BackColor = &HFF00&
        ElseIf Val(Day) < Val(ProdTarg) * (Sixth - SixthLim) Then
        Day.BackColor = &HFF&
        Else
        Day.BackColor = &H80FF&
    End If
Next Day

I've got a For Each loop, to make the coding more concise, but I keep getting a type mismatch. The If loop inside is based on the following code:

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

I hope this gives you an idea of what I'm trying to achieve. I hope I've not been too dense, but I can't seem to find a topic on how to integrate the variable in the way I require.

役に立ちましたか?

解決

To make your code working you need to do the following changes:

  1. declare ProdArray as Variant in this way:

    Dim ProdArray(0 To 5)

  2. use Set to create array element like:

    Set ProdArray(0) = MonProd
    Set ProdArray(1) = TueProd
    'etc

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top