如何在 VBA 中单击时更改形状颜色?

问题描述 投票:0回答:1

我希望在单击 Excel 中的形状时更改其颜色。 但我是 VBA 新手,不知道如何为此编写代码。 任何方法将不胜感激。

excel vba colors shapes fill
1个回答
1
投票
  1. 将此代码复制到标准模块中:
Sub MyShape_Click()
  Dim sh As Shape
   Set sh = ActiveSheet.Shapes(Application.Caller)
   If sh.Fill.ForeColor.RGB = RGB(255, 0, 0) Then
        sh.Fill.ForeColor.RGB = RGB(0, 0, 255)
   Else
        sh.Fill.ForeColor.RGB = RGB(255, 0, 0)
  End If
End Sub
  1. 右键单击讨论中的形状并选择
    Assign Macro...
    。在打开的窗口中,您应该搜索并选择上面的子宏名称 (MyShape_Click)。按“确定”并测试解决方案。它首先会将形状着色为红色,然后交替着色为蓝色和红色。
© www.soinside.com 2019 - 2024. All rights reserved.