Mike遇到了一个问题,他知道自己的工作簿中隐藏了一些对象,因此他想找到所有对象。看来他写了一个宏来隐藏一些对象,但是并没有取消隐藏它们。

如果您只想查找工作表中对象的名称,则下面的宏将非常好用。它不仅显示名称,还显示对象的类型。

Sub ListObjects()

Dim objCount As Integer     Dim x As Integer     Dim objList As String     Dim objPlural As String     Dim objType(17) As String

'Set types for different objects     objType(1) = "Autoshape"

objType(2) = "Callout"

objType(3) = "Chart"

objType(4) = "Comment"

objType(7) = "EmbeddedOLEObject"

objType(8) = "FormControl"

objType(5) = "Freeform"

objType(6) = "Group"

objType(9) = "Line"

objType(10) = "LinkedOLEObject"

objType(11) = "LinkedPicture"

objType(12) = "OLEControlObject"

objType(13) = "Picture"

objType(14) = "Placeholder"

objType(15) = "TextEffect"

objType(17) = "TextBox"



objList = ""



'Get the number of objects     objCount = ActiveSheet.Shapes.Count

If objCount = 0 Then         objList = "There are no shapes on " & _           ActiveSheet.Name     Else         objPlural = IIf(objCount = 1, "", "s")

objList = "There are " & Format(objCount, "0") _           & " Shape" & objPlural & " on " & _           ActiveSheet.Name & vbCrLf & vbCrLf         For x = 1 To objCount             objList = objList & ActiveSheet.Shapes(x).Name & _               " is a " & objType(ActiveSheet.Shapes(x).Type) _               & vbCrLf         Next x     End If

MsgBox (objList)



End Sub

此宏返回工作表中所有对象的名称和类型。

但是,另一种方法是显示所有对象名称,然后,如果对象是隐藏的,则询问是否要取消隐藏它。下面的宏就是这样做的:

Sub ShowEachShape1()

Dim sObject As Shape     Dim sMsg As String     For Each sObject In ActiveSheet.Shapes         sMsg = "Found " & IIf(sObject.Visible, _           "visible", "hidden") & " object " & _           vbNewLine & sObject.Name         If sObject.Visible = False Then             If MsgBox(sMsg & vbNewLine & "Unhide ?", _               vbYesNo) = vbYes Then                 sObject.Visible = True             End If         Else             MsgBox sMsg         End If     Next End Sub

如果希望宏仅对隐藏对象起作用,而忽略那些可见的对象,则可以将宏修改为以下内容:

Sub ShowEachShape2()

Dim sObject As Shape     Dim sMsg As String     For Each sObject In ActiveSheet.Shapes         If sObject.Visible = False Then             sMsg = "Object & sObject.Name & _               " is hidden. Unhide it?"

If MsgBox(sMsg, vbYesNo) = vbYes Then                 sObject.Visible = True             End If         End If     Next End Sub

要使所有对象一步一步可见,可以进一步缩短宏:

Sub ShowEachShape3()

Dim sObject As Shape     For Each sObject In ActiveSheet.Shapes         sObject.Visible = True     Next End Sub

注意:

如果您想知道如何使用此页面(或_ExcelTips_网站上的任何其他页面)中描述的宏,我准备了一个特殊页面,其中包含有用的信息。

_ExcelTips_是您进行经济高效的Microsoft Excel培训的来源。

本技巧(2025)适用于Microsoft Excel 97、2000、2002和2003。