VBA 编码为 hide/unhide charts/boxes

VBA code to hide/unhide charts/boxes

我在活动 sheet 中的 hide/unhide charts/list 框中写了一个 vba 代码,我分配给 "button" 或只是我指定的形状从 excel 中的“插入”选项卡中拉出。此代码适用于 1 种形状,但我无法让它适用于多种形状。我是 VBA 的新手,所以我不确定语法的所有细微差别。这是我拥有的:

Sub OverviewB()

    With ActiveSheet.Shapes("Rounded Rectangle 1").TextFrame2.TextRange.Characters

        If .Text = "Hide Overview" Then

            .Text = "Show Overview"

            ActiveSheet.Shapes("Chart 20", "List Box 1", "Chart 19", "List Box 3", "Chart 22", "List Box 4", "Chart 24", "List Box 5").Visible = False

        Else

            .Text = "Hide Overview"

                ActiveSheet.Shapes("Chart 20", "List Box 1", "Chart 19", "List Box 3", "Chart 22", "List Box 4", "Chart 24", "List Box 5").Visible = True



        End If
    End With
End Sub

我的 chart/list 框名称中间确实有一个行分隔符“_”,所以它没有那么宽,但我把它去掉了,因为我认为它可能会导致一些错误。

谁能看出语法错误在哪里?我收到的错误是: "Runtime Error 450": 参数数量错误或 属性 赋值无效。

错误消息给了您一些提示,参数数量错误或 属性 分配无效。您向 .Shapes 发送了太多参数属性.

Worksheet.Shapes property collection takes a single argument and returns a Shapes object, which is a collection of Shape objects。来自 dox,

Use Shapes(index), where index is the shape’s name or index number, to return a single Shape object.

还有:

Use Shapes.Range(index), where index is the shape’s name or index number or an array of shape names or index numbers, to return a ShapeRange collection that represents a subset of the Shapes collection.

您需要设置数组中的元素并指定范围内的项目

 ActiveSheet.Shapes.Range(Array("Chart 20", "List Box 1", "Chart 19", "List Box 3", "Chart 22", "List Box 4", "Chart 24", "List Box 5")).Visible = True