简体   繁体   English

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

[英]Hide/unhide specific objects in Excel with VBA

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)中一样?

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! 希望能帮助到你!

As the visible property is a boolean, you can shorten your code : 由于visible属性是布尔值,因此可以缩短代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM