vba 在活动工作表中显示和隐藏形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17360067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show and Hide Shapes in Active Worksheet
提问by eathapeking
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
This is the sample of code macro that i use
这是我使用的代码宏示例
i want to know how we can on run this code to hide only rectangle 1 line
我想知道我们如何运行此代码以仅隐藏矩形 1 行
like
喜欢
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes.Range(Array("Rounded Rectangle *")).Visible = _
Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle *")).Visible
Next i
I just dont know how to simplify correctly
我只是不知道如何正确简化
采纳答案by eathapeking
Youre not far off , this is how to use the loop to go through all the Rounded Rectangles
离你不远了,这是如何使用循环来完成所有的 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
note how "Rounded Rectangle " & i
is replaced with the "Rounded Rectangle 1"
"Rounded Rectangle 2"
"Rounded Rectangle n"
where n = i
( meaning the amount of times the loop runs )
注意 how"Rounded Rectangle " & i
被替换为"Rounded Rectangle 1"
"Rounded Rectangle 2"
"Rounded Rectangle n"
where n = i
(意思是循环运行的次数)
因此,如果您只想隐藏第一个
Rounded Rectangle 1
Rounded Rectangle 1
,则添加一个If/elseIf/else语句到您的循环
note笔记: this code will never display the first
Rounded Rectangle 1
and togglebetween showing all the other ones. if you wanted to always show them then just assign trueinside the else statement:此代码永远不会显示第一个Rounded Rectangle 1
并在显示所有其他代码之间切换。如果您想始终显示它们,则只需在 else 语句中分配trueDim 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