在活动工作表中显示和隐藏形状

时间:2013-06-28 08:05:56

标签: excel vba excel-vba

ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Visible
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 3")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 3")).Visible
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 4")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 4")).Visible
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 5")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 5")).Visible

这是我使用的代码宏示例

我想知道如何运行此代码以仅隐藏矩形1行

For i = 1 To ActiveSheet.Shapes.Count
    ActiveSheet.Shapes.Range(Array("Rounded Rectangle *")).Visible = _ 
    Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle *")).Visible
Next i

我只是不知道如何正确简化

1 个答案:

答案 0 :(得分:2)

离你不远,这是如何使用循环来完成所有Rounded Rectangles

Dim i As Long
For i = 1 To ActiveSheet.Shapes.Count
    ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible = _
    Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible
Next i

请注意"Rounded Rectangle " & i如何替换为 "Rounded Rectangle 1"
"Rounded Rectangle 2"
"Rounded Rectangle n"其中n = i(表示循环运行的次数)

<小时/> 因此,如果您只想隐藏第一个Rounded Rectangle 1,请在循环中添加 If / else 语句
note :此代码永远不会在显示所有其他代码之间显示第一个Rounded Rectangle 1切换。如果您想要始终显示它们,那么只需在else语句中指定 true

Dim i As Long
For i = 1 To ActiveSheet.Shapes.Count
    If i = 1 Then
        ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible = False
    Else
        ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible = _
        ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible
    End If
Next i