Toggle Button Stay pressed or unpressed even if exiting and re-entering stays where I left it before

Carlosjunior

New Member
Joined
Jun 7, 2023
Messages
7
Office Version
  1. 2021
Platform
  1. Windows
In the example of the video I posted I have a problem:

My toggle button when I exit and re-enter it stays on the userform in green color untill then all right, the problem is when I switch to red color and exit the form it automatically changes to green color instead of getting unpressed in red color where I left it before. Can you help me please. Thanking in advance
 

Attachments

  • To be unpressed.PNG
    To be unpressed.PNG
    10.2 KB · Views: 5

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
One way might be to use a worksheet cell to keep track of the state of your togglebutton. When unloading your userform, record its state in a cell, let's say A1...

VBA Code:
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Me.ToggleButton1.Value

Unload Me

Then, in the userform initialize event handler, set the state of the togglebutton according to the value in A1...

VBA Code:
Me.ToggleButton1.Value = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value

Hope this helps!
 
Upvote 0
@Carlosjunior Having looked at your previous thread, I'm thinking. maybe you wish to initialise the toggle button based on rows 2:4 being hidden or not?
 
Upvote 0
@Carlosjunior Not sure if you are now sorted or if the below helps?

My 365 Mac setup does not let me do forms or toggle buttons so its an indication of coding only that works for a checkbox.
Maybe substitute toggle button for checkbox and use logic of Init for your Form initialise code?


VBA Code:
Sub CheckBox1_Click()
Dim Rws As Range
Dim Tog As CheckBox
Dim State As Boolean

State = ActiveSheet.Range("2:4").EntireRow.Hidden
Set Tog = Sheets("Sheet1").CheckBoxes("Check Box 1")
Set Rng = Sheets("Sheet1").Range("2:4")
With Tog
    If State Then
        .Caption = "Hide"
        .Interior.Color = vbRed
       .Value = 1
    Else
        .Caption = "Hidden"
        .Interior.Color = vbGreen
        .Value = -4146
    End If
    
    Rng.EntireRow.Hidden = Not State
    .Value = Not State
End With

End Sub

VBA Code:
'This would be form initialisation
Sub Init()

Dim Rws As Range
Dim Tog As CheckBox
Dim State As Boolean

State = ActiveSheet.Range("2:4").EntireRow.Hidden
Set Tog = Sheets("Sheet1").CheckBoxes("Check Box 1")
Set Rng = Sheets("Sheet1").Range("2:4")
With Tog
    If Not State Then
        .Caption = "Hide"
        .Interior.Color = vbRed
       .Value = 0
    Else
        .Caption = "Hidden"
        .Interior.Color = vbGreen
        .Value = 1
    End If
    
End With

End Sub

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,215,307
Messages
6,124,163
Members
449,146
Latest member
el_gazar

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