0

I am looking for some code that will change the colour of a shape when a cell is clicked on. Example the shape is S_IRL which is Ireland and is located in Cell B22.

What I would like to happen is that if Cell B22 then shape S_IRL changes from Blue to Red. Then if another cell with a country is clicked then the corresponding shape changes to red and the previous returns to it previous colour. Any help would be greatly appreciated enter image description here

4

1 回答 1

1

您可以在工作表的代码中添加一个新的子例程,该子例程将在工作表上的选择更改时触发:

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 列获取形状的名称,然后将该形状的颜色设置为红色。

于 2016-04-19T20:58:42.420 回答