Control Userform labels and buttons with VB macro

Nitehawkhp

New Member
Joined
Dec 2, 2016
Messages
37
Hello,

I'm trying to control various labels on a Userform.
Some of the things I want control:

Change color of a button based on whether it has been clicked

Is it possible to make a button visible or invisible by a macro?
I have tried to use the following to accomplish this:

Code:
Label56.visible = "False" and
Label56.visible = "True"

However, it doesn't seem to do anything. The label remains visible in all cases.

Is it possible to use a OptionButton as an indicator i.e. "Active" or "Not Active"

Rod
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Rod,

The visible property is a boolean variable meaning it has to be set as VB True (value of 1) or VB False (value of 0) not a string like you have i.e. "False" and "True".

Remove the quotes and all should be good.

Robert
 
Upvote 0
Hi Rod,

The visible property is a boolean variable meaning it has to be set as VB True (value of 1) or VB False (value of 0) not a string like you have i.e. "False" and "True".

Remove the quotes and all should be good.

Robert

What is strange about this is, if I change the visible option in the properties for the button, it does nothing.
Ideas, anyone????

Rod
 
Upvote 0
What code do you have in the initialize & Activate events for the userform?
 
Upvote 0
What code do you have in the initialize & Activate events for the userform?

Code:
Private Sub UserForm_Activate()


    Dim lngCounter As Long
    
    Application.ScreenUpdating = False
    
    Range("Z3:Z16").Select
    Selection.ClearContents
    Range("A1").Select
    
    For lngCounter = 1 To 12
        Controls("TextBox" & lngCounter).Value = Sheets("Data Validation").Range("X" & lngCounter + 2).Value
    Next lngCounter
    
    Application.ScreenUpdating = True


End Sub




Private Sub UserForm_Initialize()


    Dim lngCounter2 As Long
    Dim Label, MyLabel As String
        For lngCounter2 = 56 To 78
            MyLabel = lngCounter2
            Label = "Label" & lngCounter2 'MyLabel ',lngCounter2.Visible = False
            Label.Visible = "False"
        Next lngCounter2
        
        


End Sub

For the code to open the Userform I have tried the two following code. Which both of them are the same
they are only in different modules. The first is in Module5 and the second is in Module6.

Module5
Code:
Sub UserForm_Click()'
' UserForm_Click Macro
'


'
    AdjustResetButtonForm.Show
    
    
End Sub

Module6
Code:
Sub Userform_Open_Click()'
' Userform_Open_Click Macro
'


    AdjustResetButtonForm.Show
'
End Sub
Neither of them work.

Thanks for your help.

Rod
 
Upvote 0
So to hide the label controls you just needed to change the control from "TextBox" to "Label" from my solution for you here like so (you could have even put it in the form's Activate event along my code):

Code:
Private Sub UserForm_Initialize()

    Dim lngCounter2 As Long
    
    Application.ScreenUpdating = False
    
    For lngCounter2 = 56 To 78
        Controls("Label" & lngCounter2).Visible = False
    Next lngCounter2
    
    Application.ScreenUpdating = True
    
End Sub

To open your form you just need the following macro in a standard module i.e. it's not part of the form code like what I've provided for you above:

Code:
Sub ShowForm()

    AdjustResetButtonForm.Show

End Sub

You can then run the ShowForm macro manually through the Macro dialog accessed by pressing Alt+F8 (i.e. press and hold the Alt key followed by the F8 key) or you can assign it to a button, shape, picture etc

Robert
 
Last edited:
Upvote 0
open
So to hide the label controls you just needed to change the control from "TextBox" to "Label" from my solution for you here like so (you could have even put it in the form's Activate event along my code):

Code:
Private Sub UserForm_Initialize()

    Dim lngCounter2 As Long
    
    Application.ScreenUpdating = False
    
    For lngCounter2 = 56 To 78
        Controls("Label" & lngCounter2).Visible = False
    Next lngCounter2
    
    Application.ScreenUpdating = True
    
End Sub

To open your form you just need the following macro in a standard module i.e. it's not part of the form code like what I've provided for you above:

Code:
Sub ShowForm()

    AdjustResetButtonForm.Show

End Sub

You can then run the ShowForm macro manually through the Macro dialog accessed by pressing Alt+F8 (i.e. press and hold the Alt key followed by the F8 key) or you can assign it to a button, shape, picture etc

Robert
I guess my light bulb is broken. I think I'm doing everything right. However, I can't get it to work.
open
Here is an image of where my open Userform code is located.

https://drive.google.com/open?id=1XTGFV6wJu4lq2DE7GheV8TN_iAXqiShK

What am I missing? I have tried using the Alt + F8 and I still get the error Run-time error '424': Object required.

I'm going crazy. It works and then just stops.

Thanks for any help you experts can offer me.
 
Upvote 0
The best thing would be to use a hosting site like www.box.com or www.dropbox.com and post your workbook there (devoid of any sensitive data) so we can see what's happening.
 
Last edited:
Upvote 0
The best thing would be to use a hosting site like www.box.com or www.dropbox.com and post your workbook there (devoid of any sensitive data) so we can see what's happening.

Ok here is a link to my google drive with the entire workbook included. I think most of you will find it to be pretty archaic to what you all develop, but it's just for me at home to keep track of inventory of parts.

Anyway here is the link.

https://drive.google.com/open?id=1OcOj0VwYlGgJd_yshKmv4R4oYEVCVyxK

Rod
 
Upvote 0
Rod

Try this.
Code:
Private Sub UserForm_Initialize()
Dim lngCounter2 As Long
Dim Label As String
    
        For lngCounter2 = 56 To 78
            
            Label = "Label" & lngCounter2 'MyLabel ',lngCounter2.Visible = False
            Me.Controls(Label).Visible = False
            
        Next lngCounter2
                
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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