How to save the colored button state in form after exit Excel vba?

Masta

New Member
Joined
Feb 22, 2022
Messages
33
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
I almost finish my little project for my job to automatize processes but i encounter 1 problem. I need help with coding in VBA, I don't know how to save the state of the CommandButton1 and the rest of the CommandButtons the buttons were pressed and then they changed color and that change is saved the next time I open that file so I can see that change I made. So far, I have coded the color change I wanted, but when a refresh of the form is done or Excel is exited, that change is reset and returns to its original state as if there was no change. I try with case statement but i have no luck so far. Thank you.
Example:

Private Sub CommandButton1_Click()
CommandButton1.BackColor = RGB(0, 200, 150) 'Green
Worksheets("Odgovori").Range("H6").Value = "e-Mail"
End Sub

Private Sub CommandButton2_Click()
CommandButton2.BackColor = RGB(255, 255, 0) 'Yellow
Worksheets("Odgovori").Range("H6").Value = "Adresa"
End Sub

1661217717497.png
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
When you create the Button why not set the color the way you want in the properties window?
 
Upvote 0
Unfortunately, once the Userform is unloaded, Excel cannot keep the changing state. Need to put it back programmatically as follows.

VBA Code:
Private Sub UserForm_Initialize()
    If Worksheets("Odgovori").Range("H6").Value <> "" Then
        CommandButton1.BackColor = RGB(0, 200, 150)    'Green
    End If

    If Worksheets("Odgovori").Range("H6").Value <> "" Then
        CommandButton2.BackColor = RGB(255, 255, 0)    'Yellow
    End If
End Sub
 
Upvote 0
Solution
So you do not want to set the button to always be green. But only when you click the button but then want it to stay that way? Is that correct?
 
Upvote 0
Unfortunately, once the Userform is unloaded, Excel cannot keep the changing state. Need to put it back programmatically as follows.

VBA Code:
Private Sub UserForm_Initialize()
    If Worksheets("Odgovori").Range("H6").Value <> "" Then
        CommandButton1.BackColor = RGB(0, 200, 150)    'Green
    End If

    If Worksheets("Odgovori").Range("H6").Value <> "" Then
        CommandButton2.BackColor = RGB(255, 255, 0)    'Yellow
    End If
End Sub
I will try this one ty. :)
 
Upvote 0
The initialize suggest will work but that would set the button color to a specific color every time you opened the Userform.

Another way would be to use Hide instead of Unload when you close the userform.
But that would not work when you close the workbook.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

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