【问题标题】:How can I get a group of shapes to change colors based on which one was selected?如何获得一组形状以根据选择的形状更改颜色?
【发布时间】:2020-02-07 16:32:26
【问题描述】:

我有 4 个形状,在一个组中,“客户、供应商、潜在客户、嫌疑人”。我希望能够选择一种形状来改变风格,但一次只允许其中一种成为那种风格。所以如果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

【问题讨论】:

  • 您想用RelationshipButtons 形状做什么? With ... End With 没有任何意义,因为没有涉及“RelationshipButtons”方面的任何内容......你的按钮是什么类型的控件? ActiveX 控件,还是表单控件?我认为你应该使用他们的Click 事件。我无法理解该代码是如何工作的,即使不是完美的。你不是说只有在按下其中一个按钮时才能更改样式?如果是,那么在您的代码中的哪个位置按下了按钮?如果不是,您如何想象您的代码使用了Click 事件?
  • 你的形状真的是按钮,还是普通的矩形?你是如何创造它们的?抱歉,您知道 VBA 中的按钮是什么吗?
  • 对不起,我应该更具体一些。它们是形状,我将其用作按钮。我意识到有区别。
  • RelationshipButtons 形状是一组形状;没有必要吗?
  • 需要什么?您是否至少为这些伪按钮分配了宏?

标签: vba button grouping shapes


【解决方案1】:

这是一种更简洁的方法:

'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

【讨论】:

    【解决方案2】:

    所有功劳必须归功于@Tim Williams! 我刚刚修改了他的代码,以完全满足(我理解)Michelle 的需求。看起来 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
    

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

    【讨论】:

      猜你喜欢
      • 2018-07-20
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      相关资源
      最近更新 更多