セルをクリックすると図形の色が変わるコードを探しています。例の形状は、セル B22 にあるアイルランドの S_IRL です。
私がしたいことは、セル B22 の場合、S_IRL の形状が青から赤に変わることです。次に、国の別のセルをクリックすると、対応する形状が赤に変わり、前の色が元の色に戻ります。どんな助けでも大歓迎です
ワークシートのコードに新しいサブルーチンを追加できます。このサブルーチンは、シートで選択が変更されたときに実行されます。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim strShapeName As String 'Variable to hold the shape name
Dim shp As Shape 'Variable to hold a shape object
'Detect if the click was in range A:C
If Not Intersect(Target, Range("A:C")) Is Nothing Then
'Boom... set all the shapes to blue
For Each shp In Me.Shapes
If Left(shp.Name, 2) = "S_" Then shp.Fill.ForeColor.RGB = RGB(0, 0, 255)
Next shp
'Grab the shape name from Column A
strShapeName = Cells(Target.Row, 1).Value
'Set the color of the shape to red
Shapes(strShapeName).Fill.ForeColor.RGB = RGB(255, 0, 0)
End If
End Sub
これにより、選択の変更が列 A、B、または C のセルに対するものであったかどうかが検出されます。列 A から図形の名前が取得され、その図形の色が赤に設定されます。