Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making shapes invisible/visible in excel through VBA

Tags:

excel

vba

I'm having this problem for the last few hours and I would really appreciate some help with it.

Basically, I want to be able to hide/unhide shapes depending on selections a user makes on a userform. I've broken the problem down into a very simple example. If I insert a shape called "oval 1" in a sheet and run the code:

Sub hideshape()

    With ActiveSheet

        .Shapes("Oval 1").Select

        With Selection

        .Visible = False

        End With

    End With

End Sub

the shape disappears but when I run this code

Sub unhideshape()

    With ActiveSheet

        .Shapes("Oval 1").Select

        With Selection

        .Visible = True

        End With

    End With

End Sub

I get an error "Requested Shapes are locked for Selection"

The workbook is not protected and I have tried un-ticking locked and locked text on the shape properties.

Any ideas what's causing this.

like image 942
Matthew Brophy Avatar asked Nov 06 '15 18:11

Matthew Brophy


People also ask

How do you make a shape invisible in Excel?

Select the shape or shapes that you want to make transparent. On the Shape tab, select Shape Fill > Transparency, then select the percentage you want.

How do I make shapes appear in Excel?

Add a shape in Excel, Outlook, Word, or PowerPointOn the Insert tab, click Shapes. Click the shape you want, click anywhere in the workspace, and then drag to place the shape.

How do you show and hide columns in Excel VBA?

Hide UnHide Columns in Excel Worksheet using VBA – Solution We can use EntireColumn. Hidden property of a Column. You can set the property value to TRUE if you want to hide, Set to FALSE if you want to un-hide the Columns.


2 Answers

You cannot Select a hidden object. However, you dont need to use Select at all, and it is usually not recommended. Try simply:

Sub HideShape()
    ActiveSheet.Shapes("Oval 1").Visible = False
End Sub
Sub UnhideShape()
    ActiveSheet.Shapes("Oval 1").Visible = True
End Sub
like image 196
A.S.H Avatar answered Nov 15 '22 00:11

A.S.H


Sub HideEachShape()
Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
sObject.Visible = False
Next
End Sub

from: extendoffice.com

like image 37
Armand Villa Avatar answered Nov 14 '22 23:11

Armand Villa