Change text & Background of a Toggle Button - excel

i have a toggle button that can hide me the chart but i want it to be more designed.
I would like that when the chart is hidden that the button text is 'Show' and backgroundcolor is Red and when he is show, the button is 'Hidden' and background color is Black.
This is my VBA code :
Option Explicit
Sub hidechart()
With ThisWorkbook.Worksheets("CA").Range("45:74,118:147, 192:220, 265:292")
.EntireRow.Hidden = Not .EntireRow.Hidden
End With
End Sub

If ToggleButton1.Value = True Then
Rows(4).EntireRow.Hidden = True
Rows(5).EntireRow.Hidden = True
Rows(6).EntireRow.Hidden = True
Else
Rows(4).EntireRow.Hidden = False
Rows(5).EntireRow.Hidden = False
Rows(6).EntireRow.Hidden = False
End If

Related

Using ToggleButton's false .Value to remove the grey color on top when it is clicked

I'm trying to make a ToggleButton look always black but when clicked there's always this grey mask appearing on top (probably for the "clicked" effect) :
I found that with the false .Value of the ToggleButton you can remove it but my problem is now that the userform I'm making appear is appearing again once closed. Is there a way to fix that ?
This is my sub for when you click the button :
Private Sub ToggleButton1_Click()
ToggleButton1.Value = False
ToggleButton1.Caption = "Afficher l'interface"
ToggleButton1.ForeColor = RGB(255, 255, 255)
ToggleButton1.BackColor = RGB(0, 0, 0)
UF_Interface.Show 'Userform I want to show when you click the button
End Sub
Thanks
If I may suggest, if you want to use the ToggleButton,
maybe it's better making a two condition (if true then what, if false then what), such as the image below :
So in the image above, if true then open the other UF - if false then close that UF.
Below is the code in the UF where the ToggleButton reside :
Private Sub UserForm_Initialize()
With ToggleButton1
.Caption = "OPEN UF_Interface"
.BackColor = vbYellow
End With
End Sub
Private Sub ToggleButton1_Click()
With ToggleButton1
If .Value = True Then
.Caption = "CLOSE UF_Interface"
.BackColor = vbGreen
UF_Interface.Show vbModeless
Else
.Caption = "OPEN UF_Interface"
.BackColor = vbYellow
Unload UF_Interface
End If
End With
End Sub
Both userforms (UF_Interface and the one with the toggle button) must be shown in vbModeless state. So you need to make another sub in regular module, something like below :
Sub OpenFormWithToggleButton()
FormWithToggleButton.Show vbModeless
End Sub
With the help of #karma I modified my code this way (since I couldn't change the color of a "normal" Button even though I wanted it's way of working) :
Private Sub ToggleButton1_Click()
With ToggleButton1
If .Value = True Then
.Caption = "Afficher l'interface"
.BackColor = vbBlack
.Value = False
UF_Interface.Show 'Userform I wanted to show
End If
End With
End Sub

How do I make a combobox visible/invisible depending on checkbox value?

I have a userform that contains two sets of comboboxes that depend on checkboxes each to be made visible. Upon the userform opening they are invisible. Upon checking the box the two fields become visible.
The comboboxes under the "CC MACH" checkbox work perfectly. They default to NOT visible upon loading the userform and only become visible upon checking the box...and become invisible if I uncheck the box.
But the comboboxes under neath "Label Printer", although they default to NOT visible upon loading, they do not become visible when I click the box...nor when I uncheck them.
The code that used for the CC MACH dependent boxes is the following:
Private Sub nd_ccmach_cx_Click()
If nd_ccmach_cx = True Then
nd_ccmach_cb.Visible = True
nd_ccip_lbl.Visible = True
nd_ccip_cb.Visible = True
Else
If nd_ccmach_cx = False Then
nd_ccmach_cb.Visible = False
nd_ccip_lbl.Visible = False
nd_ccip_cb.Visible = False
End If
End If
End Sub
The one for the Label Printer dependent boxes is as follows:
Private Sub nd_lblptr_cx_Click()
If nd_lblprt_cx = False Then
nd_lblptr_cb.Visible = False
nd_lblptr_usbip_lbl.Visible = False
nd_usbip_cb.Visible = False
Else
If nd_lblprt_cx = True Then
nd_lblptr_cb.Visible = True
nd_lblptr_usbip_lbl.Visible = True
nd_usbip_cb.Visible = True
End If
End If
End Sub
The Userform Initialize code is a follows:
nd_ccmach_cb.Visible = False
nd_ccip_lbl.Visible = False
nd_ccip_cb.Visible = False
nd_lblptr_cb.Visible = False
nd_lblptr_usbip_lbl.Visible = False
nd_usbip_cb.Visible = False
Can you please tell me why the "Label Printer" dependent boxes will not respond to the checkbox value?

Userform button stays pressed after command

I have Userform with buttons that are connected to codes. Why buttons keeps being pressed after code has been completed? Is there any way to release them?
Button looks like this after clicking on it:
Button code:
Private Sub ToggleButton2_Click()
ThisWorkbook.Worksheets("Price calculation").Activate
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Price calculation").Unprotect Password:="123"
Range("1867:1979").EntireRow.Hidden = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 76").Visible = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 244").Visible = True
ActiveWindow.ScrollRow = 1867
ThisWorkbook.Sheets("Price calculation").Protect Password:="123"
Application.ScreenUpdating = True
End Sub
You're using a toggle button. These toggle, unsurprisingly. I reckon you are looking to use a CommandButton instead.
In the toolbox browse for the CommandButton control Replace the ToggleButton with the newly added CommandButton. Then, edit the code for your togglebutton and replace it with:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
With ThisWorkbook.Sheets("Price calculation")
.Activate
.Unprotect Password:="123"
.Range("1867:1979").EntireRow.Hidden = False
.Shapes("Rectangle: Rounded Corners 76").Visible = False
.Shapes("Rectangle: Rounded Corners 244").Visible = True
ActiveWindow.ScrollRow = 1867 'Is this necessary? It scrolls to a specific hard-coded row
.Protect Password:="123"
End With
Application.ScreenUpdating = True
End Sub

Excel Group of image hiding on another sheet checkbox

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

How to set checked property in vba (format or control toolbox)

I'm trying to change the value of my checkbox to true based on a another cell's value
if range("A1").value = "green" then
Checkbox1.value= true
end if
How to I change the value property to true for multiple checkbox at the same time
For some reason the code that i've tried doesn't do anything at all.
P.S. I'm using format checkboxes
This will change all Checkboxes
Sub Changeboxes()
Dim cb As CheckBox
If Sheet1.Range("a1").Value = "green" Then
For Each cb In Sheet1.CheckBoxes
cb.Value = True
Next cb
End If
End Sub
If you need to specify particular checkboxes, then
Sub ChangeSomeCbs()
If Sheet1.Range("a1").Value = "green" Then
Sheet1.CheckBoxes("Check Box 1").Value = True
Sheet1.CheckBoxes("Check Box 2").Value = False
Sheet1.CheckBoxes("Check Box 3").Value = True
End If
End Sub
Checkbox and Checkboxes are hidden properties. You won't get intellisense, but they work.
This works fine for me:
If range("O26").Value = "green" Then
CheckBox1.Value = True
CheckBox2.Value = True
End If
If you are in design mode this will not work.
This code is true for Office365:
If range("O26").Value = "green" Then
CheckBox1 = True
CheckBox2 = True
End If

Resources