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.
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
I hide shapes based on their name since some shapes I don't want to hide. I use this format:
Sheet1.Shapes.Range(Array("COtxtBox1")).Visible = msoTrue
name of your shape or shapes goes into the array
if it only 1 shape you could just use:
Sheet1.Shapes.range("COtxtBox1").Visible = True
I found that the "mso" part is not necessary for the True or False statement
Sub HideEachShape()
Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
sObject.Visible = False
Next
End Sub
from: extendoffice.com
I solved problem with this code(Oval = Type 9, from MsoAutoShapeType Enumeration (Office)):
Sub hide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = False
Next i
End Sub
Sub unhide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = True
Next i
End Sub
If "Type = 9" is wrong, you can find out type of your shape with code in Immediate window (ctrl+G in VBA):
?ActiveSheet.Shapes("Oval 1").Type
Public HIDE As Boolean
Sub T_BUTTON ()
ActiveSheet.Shapes("T 1").Visible = HIDE
If ActiveSheet.Shapes("T 1").Visible = False Then
HIDE = True
Else
HIDE = False
End If
END SUB
Related
I'm creating a workbook in excel and using activeX checkboxes to hide/unhide some rows each time as a way to enable or disable them. For that I use VBA code. However in that code I specify a static range of rows. The problem is that if I insert a new row, everything is now offset and i have to manually rewrite all the intervals. Is there a way to do this dynamically.
Here is the code I use for two consecutive checkboxes:
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
[24:41].EntireRow.Hidden = False
Else: [24:41].EntireRow.Hidden = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2 = True Then
[42:49].EntireRow.Hidden = False
Else: [42:49].EntireRow.Hidden = True
End If
End Sub
I have searched for a while before asking the question, and I apologise if there is an answer somewhere, I'm a total newbie to excel and VBA and I don't think I can adapt a solution that is remotely similar to the problem I'm facing.
Thanks for your precious help in advance.
Have a Column in which you write an X in each row you want to hide / unhide.
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = False
End If
Next c
Else
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = True
End If
Next c
End If
End Sub
You could try naming your range
thisworkbook.names.add name:="mydinamicrange" ,ReferstoR1C1:="Sheet1!R24c1:R49C10"
Then , you may use it with something like this :
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden = False
Else: thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden= True
End If
I have 2 sheets on excel, one called Raw and one called Graphs. What I want to do is have some cells in Raw and it they =TRUE then I want a shape to appear on the Graphs page.
I am pretty new to VBA so i havent tried much :(
Private Sub Worksheet_Calculate()
With Worksheets("Graph")
If Me.Range("FK45").Value = True Then
.Shapes("Test1").Visible = True
Exit Sub
ElseIf Me.Range("FK45").Value = False Then
.Shapes("Test1").Visible = False
Exit Sub
End If
End With
End Sub
I can get this to work so if FK45 is TRUE the image shows but if FK45 is FALSE it doesn't, But what I want to be able to do is add more to this e.g.
Private Sub Worksheet_Calculate()
With Worksheets("Graph")
If Me.Range("FK45").Value = True Then
.Shapes("Test1").Visible = True
Exit Sub
ElseIf Me.Range("FK45").Value = False Then
.Shapes("Test1").Visible = False
Exit Sub
End If
End With
With Worksheets("Graph")
If Me.Range("FK46").Value = True Then
.Shapes("Test2").Visible = True
Exit Sub
ElseIf Me.Range("FK46").Value = False Then
.Shapes("Test2").Visible = False
Exit Sub
End If
End With
End Sub
I want them all to be independent from each other and be able to add more if necessary
If FK45 is TRUE Image1 shows
If FK45 is FALSE Image1 doesn't show
and/or
If FK46 is TRUE Image2 shows
If FK46 is FALSE Image2 doesn't show
and/or
If FK47 is TRUE Image3 shows
If FK47 is FALSE Image3 doesn't show
and so on...
This is how I would do it
In VB Editor find your Worksheet and Shape objects and rename their system name to something intutive in their respective property windows.
This way you can get rid of With Worksheets("Graph") construction, instead you can call them by their system name like With Graph. This will also come in handy if you wish to rename your worksheets or shapes.
Note that you Exit Sub after each cell check, your procedure stops after first cell and doesn't proceed any further.
Instead of Worksheet.Calculate event I advise using Worksheet.Change. This way you can iterate through your cells one by one.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngCell As Range
Dim shpImage As Shape
For Each rngCell In Target.Cells
' check if change was made in "FK"
If Not Intersect(rngCell, Me.Columns("FK")) Is Nothing Then
Select Case rngCell.Row
Case 45: Set shpImage = Graph.MyImage_1
Case 46: Set shpImage = Graph.MyImage_2
End Select
' if only boolean values present, no need for IF construction
shpImage.Visible = rngCell.Value : Set shpImage = Nothing
End If
Next
End Sub
If you had separate column for image name it would be much easier, you could check like this (for example, shape names are located in "FL" column)
Graph.Shapes(rngCell.Offset(0, 1).Value).Visible = rngCell.Value
I'm currently in the process of building a dashboard in excel 2016 using VBA and macros. It basically involves selecting buttons on a single sheet to show/hide different chart elements whose data sources will be linked to another sheet. Its going well so far, but I've run into efficiency issues. The first issue has to do with grouping objects and the second to do with repetition of vba code.
1) Is there any way to cross-group shape objects? Right now it seems that I have to create duplicates for objects which will not be visible, and for which each parent group of objects will be hidden. Unfortunately, I cannot find a way for Excel to cross group. So if I want a group A and B and a group A and C, I can only apparently have one group (A&B or A&C, but not both). This forces me to duplicate objects so I have one pair for A&B and another for A&C. The problem is this can get quite tedious and a nightmare in organizing my objects properly when I get several dashboard buttons and sub-sections.
2) Could anyone advise on how I can make the following code more efficient so there is no repeating of code? It basically highlights a group of shapes that must show and hide with respect to what button is pressed. But right now, I have to write all the groups that must stay visible and hidden. Below is a sample of the code. Please see the True and False arguments below to get an idea of what the issue is:
Sub Pic_1_SA_click()
ActiveSheet.Shapes("Group 23").Visible = True
ActiveSheet.Shapes("Group 71").Visible = False
ActiveSheet.Shapes("Group 19").Visible = False
ActiveSheet.Shapes("Group 20").Visible = False
End Sub
Sub Pic_1_SB_click()
ActiveSheet.Shapes("Group 23").Visible = False
ActiveSheet.Shapes("Group 71").Visible = True
ActiveSheet.Shapes("Group 19").Visible = False
ActiveSheet.Shapes("Group 20").Visible = False
End Sub
Sub Pic_2_SA_click()
ActiveSheet.Shapes("Group 23").Visible = False
ActiveSheet.Shapes("Group 71").Visible = False
ActiveSheet.Shapes("Group 19").Visible = True
ActiveSheet.Shapes("Group 20").Visible = False
End Sub
Sub Pic_2_SB_click()
ActiveSheet.Shapes("Group 23").Visible = False
ActiveSheet.Shapes("Group 71").Visible = False
ActiveSheet.Shapes("Group 19").Visible = False
ActiveSheet.Shapes("Group 20").Visible = True
End Sub
Any guidance on these two issues will be greatly appreciated. Thanks!
I simple code refactoring should do it
Sub Pic_SA_click(ByVal sShapeName As String)
If (sShapeName = vbNullString) Then Exit Sub
Dim oShp As Shape, s As Shape
Set oShp = ActiveSheet.Shapes(sShapeName)
If (oShp Is Nothing) Then Exit Sub
oShp.Visible = True
With ActiveSheet
For Each s In .Shapes
If (s.Name <> oShp.Name) Then
s.Visible = False
End If
Next s
End With
End Sub
Sub Pic_1_SA_click()
call Pic_SA_click("Group 23")
end sub
Sub Pic_1_SB_click()
call Pic_SA_click("Group 71")
End Sub
Sub Pic_2_SA_click()
call Pic_SA_click("Group 19")
End Sub
Sub Pic_2_SB_click()
call Pic_SA_click("Group 20")
End Sub
I'm trying to hide a bunch of images i have placed into a group via a checkbox, I can do this via the same sheet but no on the sheet the textbox is.
Sub hideimages()
If ActiveSheet.CheckBoxes("Check Box 1").Value = 1 Then
ActiveSheet.Shapes("Group 21").Visible = True
Else: ActiveSheet.Shapes("Group 21").Visible = False
End If
End Sub
But i can't seem to figure out the right syntax to get it to affect another sheet for the group I can do it for a singular image:
Sub CheckBox33_Click()
Dim obj As Shape
Set obj = Worksheets("sheet3").Shapes("picture 2")
If obj.Visible Then
obj.Visible = True
Else
obj.Visible = False
End If
How could i merge these? the ways i have tried are not happy!
Sub hidaway()
If Worksheets("sheet1").CheckBoxes("Check Box 34").Value = 1 Then
Worksheets("sheet3").group("Group 21").Visible = True
Else: Worksheets("sheet3").group("Group 21").Visible = False
End If
End Sub
Your checkbox returns True/False so you just need to feed this value to your group visible property:
Private Sub CheckBox1_Click()
ThisWorkbook.Worksheets("Sheet3").Shapes("Group 21").Visible = Me.CheckBox1.Value
End Sub
Heres the Code that I put in the module1 from Tim Williams
Sub Tester()
Dim isOn As Boolean
With ActiveSheet
Application.Caller = MuddyBoots
isOn = (.CheckBoxes(Application.Caller).Value = xlOn)
.CheckBoxes("TabletUser").Visible = isOn
.CheckBoxes("WebUser").Visible = isOn
End With
End Sub
I have three checkboxes:
MuddyBoots
TabletUser
WebUser
When MuddyBoots is ticked I want TabletUser and WebUser to be visible and when MuddyBoots is unticked I want the two checkboxes TabletUser and WebUser to be invisible.
The code that works-ish is below:
Public Sub TestCheckbox()
Dim s As Shape
Set s = ActiveSheet.Shapes("MuddyBoots")
s.Select
If Selection.Value = xlOn Then
MsgBox "Checked"
ActiveSheet.CheckBoxes("TabletUser").Visible = True
ActiveSheet.CheckBoxes("WebUser").Visible = True
'code here
Else
MsgBox "Not checked"
ActiveSheet.CheckBoxes("WebUser").Visible = False
ActiveSheet.CheckBoxes("TabletUser").Visible = False
'code here
End If
End Sub
This removes the two checkboxes if MuddyBoots is unticked after you ok the pop up message. If I comment out the msgbox line it doesn't work. Then I have to restart the VBA code.
These are form controls.
I also get the error message: Cannot run the macro New User Form Macro'!CheckBox17_Click'. The macro may not be available in this workbook or all macros may be disabled. I have changed the name of the checkboxs in the name box and have triple checked the checkboxes and they have the correct names...
I want to know how to get this functionality working without the popup messages and without the error please.
Sub Tester()
Dim isOn As Boolean
With ActiveSheet
'Application.Caller = name of calling shape
isOn = (.CheckBoxes(Application.Caller).Value = xlOn)
.CheckBoxes("TabletUser").Visible = isOn
.CheckBoxes("WebUser").Visible = isOn
End With
End Sub