Mike had a problem where he knew that there were objects hidden within his workbook and he wanted to find them all. It seems he wrote a macro that hid some objects, but then did not unhide them.
If you want to simply find out the names of the objects in a worksheet, the following macro will do so very nicely. It shows not only the name, but also the type of object.
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
This macro returns the names and types of all objects in the worksheet. Another approach, however, is to display all the object names and then, if the object is hidden, ask if you want it unhidden. The following macro does just that:
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
If you want the macro to only work on hidden objects and ignore those that are visible, then you can modify the macro to the following:
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
To simply make all the objects visible in one step, you can shorten the macro even more:
Sub ShowEachShape3() Dim sObject As Shape For Each sObject In ActiveSheet.Shapes sObject.Visible = True Next End Sub
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2025) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...
Discover MoreNamed ranges are a great tool to use in developing formula-heavy workbooks. You may want, at some point, to copy your ...
Discover MoreIf your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2016-08-12 04:00:43
Ali Zolfaghari
Hello Dear Allen,
I urgently need to loop throuh activex control in all my 6 worksheets during closing the workbook(which is a questionnaire) by applicants. In every sheet I have a lot of optionbuttons and some few text cells(I mean cells that Applicants must write some text into it. I want to be sure that all optionbuttons and text cells have filled. I used the code below to loop through active sheet for looping though optionbuttons but nothing happens:
Public Sub LoopThroughControls()
Dim Sht As Worksheet
Dim Ctl As OptionButton
Dim i as integer
For i=1 to 6
worksheets("Phasei-Questions-Compare").activate ' the name of my worksheets for example Phase1-Questions-Compare
Set Sht = ActiveSheet
For Each Ctl In Sht.OptionButtons
If Ctl.Value = True
Next Ctl
Next
End Sub
If You respectfully help me solve it you have done a great favor to me.
Sincerely Yours
Ali
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments