我怎样才能得到一组形状来根据选择的形状改变颜色?

How can I get a group of shapes to change colors based on which one was selected?

我有 4 个形状,在一组中,'Customer, Vendor, Prospect, Suspect.'我希望能够 select 一个形状来改变样式,但一次只允许其中一个成为该样式.所以如果 CustomerStyle 是 msoShapeStyle31,那么我希望其他 3 个都是 msoShapeStyle32;但如果用户单击其他 3 个按钮之一,该按钮应更改为 msoShapeStyle31,其余 3 个按钮将转换为 msoShapeStyle32。我希望这是有道理的。

RelationshipButtons 是组,我计划根据其中一个形状是 msoShapeStyle31 来输出单元格值。

这是我的,但这是不对的,因为他们中的几个人同时转向 msoShapeStyle31,而一次只能转向一个。有什么帮助吗?

Sub Button_Colors()
With Sheet1

Dim CustomerButton As Shape, VendorButton As Shape, ProspectButton As Shape, SuspectButton As Shape, RelationshipButtons As Shape
Set CustomerButton = .Shapes("CustomerButton")
Set VendorButton = .Shapes("VendorButton")
Set ProspectButton = .Shapes("ProspectButton")
Set SuspectButton = .Shapes("SuspectButton")
Set RelationshipButtons = .Shapes("RelationshipButtons")

Dim CustomerStyle As Integer, VendorStyle As Integer, ProspectStyle As Integer, SuspectStyle As Integer
CustomerStyle = CustomerButton.ShapeStyle
VendorStyle = VendorButton.ShapeStyle
ProspectStyle = ProspectButton.ShapeStyle
SuspectStyle = SuspectButton.ShapeStyle

With RelationshipButtons
    If CustomerStyle = 31 Then
        CustomerStyle = msoShapeStylePreset32
        VendorStyle = msoShapeStylePreset31
        ProspectStyle = msoShapeStylePreset31
        SuspectStyle = msoShapeStylePreset31
    ElseIf VendorStyle = 31 Then
        CustomerStyle = msoShapeStylePreset31
        VendorStyle = msoShapeStylePreset32
        ProspectStyle = msoShapeStylePreset31
        SuspectStyle = msoShapeStylePreset31
    ElseIf ProspectStyle = 31 Then
        CustomerStyle = msoShapeStylePreset31
        VendorStyle = msoShapeStylePreset31
        ProspectStyle = msoShapeStylePreset32
        SuspectStyle = msoShapeStylePreset31
    ElseIf SuspectStyle = 31 Then
        CustomerStyle = msoShapeStylePreset31
        VendorStyle = msoShapeStylePreset31
        ProspectStyle = msoShapeStylePreset31
        SuspectStyle = msoShapeStylePreset32
    End If
End With
    CustomerButton.ShapeStyle = CustomerStyle
    VendorButton.ShapeStyle = VendorStyle
    ProspectButton.ShapeStyle = ProspectStyle
    SuspectButton.ShapeStyle = SuspectStyle
End With
End Sub

这是一种更简洁的方法:

'all the shpes in the group "Group 6" are assigned this macro
Sub ToggleShapes()
    Dim shp As Shape, clr
    clr = Application.Caller  '<< this is the name of the clicked shape
    Debug.Print clr
    'loop over the grouped shapes and set the color according to their name
    For Each shp In ActiveSheet.Shapes("Group 6").GroupItems
        shp.Fill.ForeColor.RGB = IIf(shp.Name = clr, vbRed, vbYellow)
    Next shp
End Sub

所有功劳必须归功于@Tim Williams! 我只是调整了他的代码来完全满足(我理解的)米歇尔的需要。看起来 Michelle 现在才明白形状和按钮之间的区别,我怀疑她能否按照她开始的方式改编这段 Tim 的好代码。

正如 Tim Williams 所说,您必须将此宏分配给所有涉及的形状。宏必须存在于模块中。该代码不会检查矩形名称是否是您发布的名称。它假设(在此变体中)它们是...

Sub ToggleShapes()
    Dim clr As String, arrNames As Variant, El As Variant
    arrNames = Array("CustomerButton", "VendorButton", "ProspectButton", "SuspectButton")
    clr = Application.Caller
        For Each El In arrNames
            If ActiveSheet.Shapes(El).Name = clr Then
                ActiveSheet.Shapes(El).ShapeStyle = msoShapeStylePreset32
                'do something else if the case...
            Else
                ActiveSheet.Shapes(El).ShapeStyle = msoShapeStylePreset31
            End If
        Next
End Sub

我还假设代码的唯一目的不仅仅是为背景着色。我想,除此之外,它还必须做点什么...