I am trying to build a dashboard in Excel using shapes that will change the foreground color based on a value. I have two sheets, Performance Data and Integrated Design. Right now I have 2 shapes that I would like to have change color bed on the values in Performance Data Cells G2 and G3; based on if the text in the cell is Red Yellow or Green. I gave a For Next Loop that will loop through the cells just fine, but I cannot seem to use a variable in the Worksheets.Shapes.Range(Array("")).Select.
The loop works fine if I hardwire the shape name in the Worksheets.Shapes.Range(Array("")).Select lines, it goes through the loop and changes color based on cell G3.
I tried naming the shapes 2 and 3 but the DC in the Worksheets.Shapes.Range(Array("")).Select causes an error.
Is there a way to assign a variable and use that variable to loop through a set of shape names in a for next loop?
Here is my code that errors out:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim PD As Variant
Dim i As Variant
Dim DC As String
For i = 2 To 3
PD = Worksheets("Performance Data").Range("G" & i)
DC = i
If PD = "Red" Then
Worksheets("Integrated Design").Shapes.Range(Array("DC")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
If PD = "Green" Then
Worksheets("Integrated Design").Shapes.Range(Array("DC")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0)
Else
If PD = "Yellow" Then
Worksheets("Integrated Design").Shapes.Range(Array("DC")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 0)
End If
End If
End If
Next i
Range("A1").Select
End Sub
The loop works fine if I hardwire the shape name in the Worksheets.Shapes.Range(Array("")).Select lines, it goes through the loop and changes color based on cell G3.
I tried naming the shapes 2 and 3 but the DC in the Worksheets.Shapes.Range(Array("")).Select causes an error.
Is there a way to assign a variable and use that variable to loop through a set of shape names in a for next loop?
Here is my code that errors out:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim PD As Variant
Dim i As Variant
Dim DC As String
For i = 2 To 3
PD = Worksheets("Performance Data").Range("G" & i)
DC = i
If PD = "Red" Then
Worksheets("Integrated Design").Shapes.Range(Array("DC")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
If PD = "Green" Then
Worksheets("Integrated Design").Shapes.Range(Array("DC")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0)
Else
If PD = "Yellow" Then
Worksheets("Integrated Design").Shapes.Range(Array("DC")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 0)
End If
End If
End If
Next i
Range("A1").Select
End Sub