用宏更改颜色形状

时间:2015-05-06 20:57:37

标签: vba excel-vba excel

我想用宏来改变形状的颜色。

ActiveSheet.Shapes.Range(Array("Rectangle 20")).Select
If Selection.ShapeRange.Fill.Visible = msoFalse Then
    Selection.ShapeRange.Fill.Visible = msoTrue
    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
    Selection.ShapeRange.Fill.Transparency = 0
    Selection.ShapeRange.Fill.Solid
Else: Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Visible = msoFalse
End If

If Selection.ShapeRange.Fill.Visible = msoTrue Then
Selection.ShapeRange.Fill.Visible = msoFalse
Else: Selection.ShapeRange.Fill.Visible = msoFalse
    Selection.ShapeRange.Fill.Visible = msoTrue
    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
    Selection.ShapeRange.Fill.Transparency = 0
    Selection.ShapeRange.Fill.Solid
End If

现在我想要的是,当没有填充物变红时,填充物为红色则不会填充。 有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

Sub ToggleMe2Colors()
    Dim sShape As Shape
    Set sShape = ActiveSheet.Shapes(Application.Caller)

    With sShape.Fill
        If .ForeColor.RGB = RGB(255, 0, 0) = True Then
            .ForeColor.RGB = RGB(0, 176, 80)
        Else
            .Visible = True
            .ForeColor.RGB = RGB(255, 0, 0)
        End If
    End With
End Sub

答案 1 :(得分:0)

试试这个:

Dim wsh As Worksheet: Set wsh = ActiveSheet
Dim shp As Shape: Set shp = wsh.Shapes("Rectangle 20")

With shp.Fill
    .Visible = Not .Visible ' Toggle fill on/off
    .ForeColor.RGB = RGB(255, 0, 0) ' Set the color
End With