使用 VBA 在 Excel 中隐藏/取消隐藏特定对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42503030/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:12:18  来源:igfitidea点击:

Hide/unhide specific objects in Excel with VBA

excelvbaexcel-vba

提问by Selrac

I want to have a macro to hide/unhide callouts.

我想要一个宏来隐藏/取消隐藏标注。

The intention is to have an information button that once presses show or hide the information callouts.

目的是有一个信息按钮,一旦按下显示或隐藏信息标注。

The problem is that I have other arrows and shapes that I don't want to be hidden.

问题是我还有其他不想隐藏的箭头和形状。

With the following code (1) I can hide all objects:

使用以下代码(1)我可以隐藏所有对象:

Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
    If sObject.Visible = False Then
        sObject.Visible = True
    Else
        sObject.Visible = False
    End If
Next

And with this code (2) I can hide/unhide specific callout shapes

使用此代码 (2) 我可以隐藏/取消隐藏特定标注形状

If ActiveSheet.Shapes("Rectangular Callout 6").Visible = False Then
    ActiveSheet.Shapes("Rectangular Callout 6").Visible = True
Else
    ActiveSheet.Shapes("Rectangular Callout 6").Visible = False
End If

How can I have the first code (1) to run through the callout shapes only like in the second code (2)?

我怎样才能让第一个代码 (1) 像第二个代码 (2) 一样运行标注形状?

采纳答案by Mihai Ovidiu Dr?goi

How about:

怎么样:

Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
   If Not InStr(sObject.Name, "Callout") = 0 Then sObject.Visible = Not sObject.Visible
Next sObject

Hope it helps!

希望能帮助到你!

回答by R3uK

As the visible property is a boolean, you can shorten your code :

由于可见属性是一个布尔值,您可以缩短代码:

Sub InvertAllShapesVisibility(wS As Worksheet)
    Dim sObject As Shape
    '''Invert visibility of all shapes
    For Each sObject In wS.Shapes
        sObject.Visible = Not sObject.Visible
    Next sObject
End Sub

How to use it :

如何使用它 :

Sub Test1_Selrac()
    InvertAllShapesVisibility ActiveSheet
End Sub


And for a single shape :

对于单个形状:

Sub RevertShapeVisibility(wS As Worksheet, ShapeName As String)
    Dim sObject As Shape
    '''Invert visibility of all shapes containing the KeyWord
    For Each sObject In wS.Shapes
        If sObject.Name = ShapeName Then sObject.Visible = Not sObject.Visible
    Next sObject
End Sub

How to use it :

如何使用它 :

Sub Test2_Selrac()
    RevertShapeVisibility ActiveSheet, "Rectangular Callout 6"
End Sub


And for multiple shapes containing keywords :

对于包含关键字的多个形状:

Sub RevertCalloutsVisibility(wS As Worksheet, KeyWord As String)
    Dim sObject As Shape
    '''Invert visibility of one shape
    For Each sObject In wS.Shapes
        If Instr(1,sObject.Name,KeyWord) Then sObject.Visible = Not sObject.Visible
    Next sObject
End Sub

How to use it :

如何使用它 :

Sub Test3_Selrac()
    RevertCalloutsVisibility ActiveSheet, "Rectangular Callout"
End Sub