hiding shapes not working on ovals

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, I am trying to use the following codes to hide/show shapes on a worksheet. The weird thing is that the code for the rectangles is working perfectly, but the code for the ovals does not. I have confirmed that the shapes are "ovals" so I am at a loss as to why it will not hide the ovals. I appreciate any insights - thanks,

VBA Code:
Sub ToggleEmergencyLights()

Dim shp As Shape

    Sheets("Device Map").Activate
    For Each shp In Sheets("Device Map").Shapes
    If shp.Type = msoShapeRectangle Then
    If shp.Name Like "*Rectangle*" Then
    If shp.Visible Then
    shp.Visible = False

Else

    shp.Visible = True

    End If
    End If
    End If
Next
End Sub


VBA Code:
Sub ToggleFireExtinguishers()

   Dim shp As Shape
   
    Sheets("Device Map").Activate
    For Each shp In Sheets("Device Map").Shapes
    If shp.Type = msoShapeOval Then
    If shp.Name Like "*oval*" Then
    If shp.Visible Then
    shp.Visible = False

Else

    shp.Visible = True

    End If
    End If
    End If

Next
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Why worry about the type if you know the name of the shapes you want to hide?
FWIW, I just drew 2 ovals on a sheet and their type is not msoshapeoval. Their type is 1 but I'd have to look for what the mso constant equivalent would be. Or you could use a message box to get the Type value and use that instead.

When you post code, proper indentation is much appreciated.

EDIT - type 1 is a rectangle

clearly they don't look like rectangles
1676930739115.png
 
Last edited:
Upvote 0
Here's how to condense and indent that code. NOTE - text comparisons are often case sensitive. Your code may not be working because the names are not "oval" something, they are "Oval" something.
VBA Code:
Sub ToggleFireExtinguishers()
Dim shp As Shape

For Each shp In Sheets("Device Map").Shapes
   If shp.Name Like "*Oval*" Then shp.Visible = shp.Visible = False
Next
End Sub
You could always default to using Upper case for comparisons:
If UCase(shp.Name) Like "*OVAL*" Then
Also, you don't have to Activate or even Select a sheet most of the time.
 
Upvote 0
Here's how to condense and indent that code. NOTE - text comparisons are often case sensitive. Your code may not be working because the names are not "oval" something, they are "Oval" something.
VBA Code:
Sub ToggleFireExtinguishers()
Dim shp As Shape

For Each shp In Sheets("Device Map").Shapes
   If shp.Name Like "*Oval*" Then shp.Visible = shp.Visible = False
Next
End Sub
You could always default to using Upper case for comparisons:
If UCase(shp.Name) Like "*OVAL*" Then
Also, you don't have to Activate or even Select a sheet most of the time.
ok - sorry to bother you, my apologies
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,470
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top