单击单元格时更改形状颜色
Change Shape Colour when Cell is Clicked
我正在寻找一些可以在单击单元格时更改形状颜色的代码。
例如,形状是 S_IRL,它是爱尔兰,位于单元格 B22 中。
我希望发生的情况是,如果单元格 B22 然后形状 S_IRL 从蓝色变为红色。然后,如果单击另一个带有国家/地区的单元格,则相应的形状将变为红色,而之前的 returns 将变为之前的颜色。
任何帮助将不胜感激
您可以在作品sheet 的代码中添加一个新的子例程,它将在 sheet 上的选择发生变化时触发:
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 列中获取形状的名称,然后将该形状的颜色设置为红色。
我正在寻找一些可以在单击单元格时更改形状颜色的代码。 例如,形状是 S_IRL,它是爱尔兰,位于单元格 B22 中。
我希望发生的情况是,如果单元格 B22 然后形状 S_IRL 从蓝色变为红色。然后,如果单击另一个带有国家/地区的单元格,则相应的形状将变为红色,而之前的 returns 将变为之前的颜色。
任何帮助将不胜感激
您可以在作品sheet 的代码中添加一个新的子例程,它将在 sheet 上的选择发生变化时触发:
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 列中获取形状的名称,然后将该形状的颜色设置为红色。