Excel Group of image hiding on another sheet checkbox - excel

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

Related

Hide multiple worksheets and diplay only one worksheet vba

I am trying to secure my workbook, i have multiple sheets that i need to hide and leave only one sheet to be displayed that will have a command button (Picture 1) when I click on it I have a userform that pops up (Picture 2) with username and password to open specific sheets( ive set different usernames and passwords to open specific sheet)
enter image description here
enter image description here
I wrote this code :
Private Sub CommandButton1_Click()
Dim User, Pass As String
User = Me.TextBox1.Text
Pass = Me.TextBox2.Text
If User = "Admin" And Pass = "123" Then
MsgBox ("Bienvenu")
Application.Visible = True
ActiveWorkbook.Unprotect Password:="password"
Sheets("ACCEUIL").Visible = True
Sheets("Liste Personnes").Visible = True
Sheets("Liste IT").Visible = True
Sheets("Liste PE").Visible = True
Sheets("Liste EM").Visible = True
Sheets("Liste ELC").Visible = True
Sheets("Liste Habilitation").Visible = True
Sheets("Liste EPC").Visible = True
Sheets("Liste ECH").Visible = True
ActiveWorkbook.Protect Password:="password"
Unload Me
Else
'Pole Essais
If User = "Admin1" And Pass = "456" Then
MsgBox " Bienvenue"
Application.Visible = True
ActiveWorkbook.Unprotect Password:="password"
Sheets("Liste Personnes").Visible = True
Sheets("Liste IT").Visible = True
Sheets("Liste PE").Visible = True
Sheets("Liste EM").Visible = True
Sheets("Liste ELC").Visible = True
Sheets("Liste Habilitation").Visible = True
Sheets("Liste EPC").Visible = True
Sheets("Liste ECH").Visible = True
Sheets("PICHON Franck").Visible = True
Sheets("MAGNIER Jean-Fran?ois").Visible = True
Sheets("LAPIERRE Louis").Visible = True
Sheets("HOSSAERT Didier").Visible = True
Sheets("DEBEYER Nicolas").Visible = True
Sheets("GARCIA Manuel").Visible = True
Sheets("GIRARD Sunny").Visible = True
Sheets("SICOT Thimot?e").Visible = True
Sheets("BEAUVILLAIN Maxime").Visible = True
Sheets("WATTEZ Eric").Visible = True
Sheets("PROUVOST Thomas").Visible = True
Sheets("PROUVOST Mathieu").Visible = True
Sheets("GARCIA Manuel").Visible = True
ActiveWorkbook.Protect Password:="password"
Unload Me
Else
MsgBox " Verifier le nom d'utilisateur ou le mot de passe"
End If
End If
End Sub
Private Sub CommandButton2_Click()
Unload Me
ActiveWorkbook.Close True
End Sub
`
it doesnt work as I want the problem is when I close my workbook and reopen it I have the recent sheets That ive been working on displayed on my workbook, and I want the worksheet "ACCEUIL" to be displayed not those Ive been working on the last time I opened the workbook.
thanks #karma it works very well, but I have something to ask, when i open my workbook i have a userform that pops up to edit/ read my database content when I finish working on my database and close the userform (UF_Choix_Service) i have all the worksheets visible again :/ but when im about to close the file I see in the background that the sheets have disappeared so your code is working good i just want to know how to hide the sheets after closing the userform.
My code to display the userfom is : (which I placed in a workbook module)
Sub Workbook_Open()
Gestion_Comp
end sub
where
Sub_Gestion_Comp ()
Application.ScreenUpdating = False
Nom_Classeur = ThisWorkbook.Name
mode_edition = False
UF_Choix_Service.Show
Select Case MsgBox("Voulez vous sauvegarder le fichier ?", vbYesNo + vbQuestion, "Sauvegarde du fichier")
Case vbYes
ThisWorkbook.Save
End Select
End Sub
enter image description here
Please have a look at this code,
maybe you can implement it to your needs.
Sub test()
Dim arr() As Variant
'get all sheet name except "Sheet1" to array
For Each sh In Worksheets
If sh.Name = "Sheet1" Then
Else
ReDim Preserve arr(X)
arr(X) = sh.Name
X = X + 1
End If
Next
'hide all the sheet name in that array, so the only not hidden is Sheet1
For Each sh In arr
If Sheets(sh).Visible = True Then Sheets(sh).Visible = False
Next
'show all the sheet name in that array
For Each sh In arr
If Sheets(sh).Visible = False Then Sheets(sh).Visible = True
Next
End Sub
I want the worksheet "ACCEUIL" to be displayed not those Ive been
working on the last time I opened the workbook.
Put your macro on the workbook module, something like this :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim arr() As Variant
For Each sh In Worksheets
If sh.Name = "ACCEUIL" Then
Else
ReDim Preserve arr(X)
arr(X) = sh.Name
X = X + 1
End If
Next
For Each sh In arr
If Sheets(sh).Visible = True Then Sheets(sh).Visible = False
Next
End Sub
You need to save the workbook before you close it.
That's if I'm not mistaken what you want.
EDIT:
when I finish working on my database and close the userform
(UF_Choix_Service). I just want to know how to hide the sheets after closing the userform
If you want all the sheets hidden but the ACCEUIL sheet, then on the Sub where you have the line : Unload UF_Choix_Service try to put this line :
Unload UF_Choix_Service '---> this is your existing code to close the userform
For Each sh In Worksheets
If sh.Name = "ACCEUIL" Then
else
If Sheets(sh.Name).Visible = True Then Sheets(sh.Name).Visible = False
end if
Next
end sub
After the code unload/close the userform, the code above say :
Loop through all the existing worksheets,
If the sheet name is "ACCEUIL", do nothing
other than ACCEUIL,
if this other sheet is visible then hide it.
Don't forget to remove the sub I ask you to put in the workbook module (BeforeClose).

Excel 2016 VBA - Creating Dashboard with greater efficiency

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

Making shapes invisible/visible in excel through 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.
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

Cant Change DropDown text while processing another DropDown

I have 2 DropDowns on an excel sheet which work independently of each other. The selections on both drop down's can cause confusion during reporting generation. So I need to set DropDown#1 to "select" if DropDown2 is being used by the user and vice versa.
I am trying to use the DropDown.Text property but it does not do the trick.
Sub PDropDown_Click()
Dim DropDownP As DropDown
Dim DropDownD As DropDown
Set DropDownD = Me.DropDowns("DDropDown")
Set DropDownP = Me.DropDowns("PDropDown")
DropDownD.Text ="Select"
DropDownP.Text = DropDownP.List(DropDownP.ListIndex)
Call Report_Generator.Create_Graph(DropDownP.List(DropDownP.ListIndex))
End Sub
You will have to add the value select to you combo boxes.
Dim bExit as Boolean
Private Sub ComboBox1_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox2.Text = "Select"
End Sub
Private Sub ComboBox2_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox1.Text = "Select"
End Sub

Excel VBA Macros Using Checkboxes To Hide Rows - If Else Statements

I have 60 check boxes on one worksheet. They are form control checkboxes. They all do a similar function which is hide rows. When you click the check box it shows the rows
When unchecked the rows are hidden. Is there an easy if else or case statement I could write for this:
Sub CheckBox1_Click()
If Range("B4").Value = True Then
Rows("5:62").EntireRow.Hidden = False
Else
Rows("5:62").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox2_Click()
If Range("B63").Value = True Then
Rows("64:102").EntireRow.Hidden = False
Else
Rows("64:102").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox3_Click()
If Range("B103").Value = True Then
Rows("104:129").EntireRow.Hidden = False
Else
Rows("104:129").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox4_Click()
If Range("B130").Value = True Then
Rows("131:160").EntireRow.Hidden = False
Else
Rows("131:160").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox5_Click()
If Range("B161").Value = True Then
Rows("162:183").EntireRow.Hidden = False
Else
Rows("162:183").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox6_Click()
If Range("B184").Value = True Then
Rows("185:222").EntireRow.Hidden = False
Else
Rows("185:222").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox7_Click()
If Range("B223").Value = True Then
Rows("224:244").EntireRow.Hidden = False
Else
Rows("224:244").EntireRow.Hidden = True
End If
End Sub
........etc etc
Also, if I need to add more rows in or delete rows, how would I code that in?
I would appreciate a format that I could use to create my own check boxes in the future without having to manually fix and edit them individually
You could use something like this:
Sub CheckBox1_Click()
HideRows "B63", "64:102"
End Sub
Sub HideRows(controllerRange, rowRange)
Rows(rowRange).EntireRow.Hidden = Not Range(controllerRange).Value
End Sub
You could also call HideRows directly from the checkbox.
In CheckBox1 assign macro, you would enter the following:
'HideRows "B63","64:102"'
Note: The single quotes MUST be entered as shown.

Resources