VBA Userform Control("ComboBox" & i) issue and calling moduel

finaljustice

Board Regular
Joined
Oct 6, 2010
Messages
175
Hello,
Why won't "control()" work inside a moduel which is called uppon from a click action of a button??
Basically this code works fine: (inside a module and the userform button calls this macro)
Code:
Sub test1()
    If UserForm1.CommandButton1.Caption = "Unlock" Then
        UserForm1.ComboBox2.Enabled = True
        UserForm1.ComboBox3.Enabled = True
        UserForm1.ComboBox4.Enabled = True
        UserForm1.TextBox19.Enabled = True
        UserForm1.CommandButton1.Caption = "Lock"
        
    ElseIf UserForm1.CommandButton1.Caption = "Lock" Then
        UserForm1.ComboBox2.Enabled = False
        UserForm1.ComboBox3.Enabled = False
    UserForm1.ComboBox4.Enabled = False
        UserForm1.TextBox19.Enabled = False
        UserForm1.CommandButton1.Caption = "Unlock"
    
    
    End If

But if I tried it like so:
Code:
Sub test1()
If UserForm1.CommandButton1.Caption = "Unlock" Then        For i = 2 To 4
            Controls("ComboBox" & i).Enabled = True
        Next
        
        UserForm1.TextBox19.Enabled = True
        UserForm1.CommandButton1.Caption = "Lock"
        
    ElseIf UserForm1.CommandButton1.Caption = "Lock" Then
        For i = 2 To 4
            Controls("ComboBox" & i).Enabled = False
        Next
        
        UserForm1.TextBox19.Enabled = False
        UserForm1.CommandButton1.Caption = "Unlock"
    
    End If
End Sub

If I use this code above INSIDE the button code it works. Am I missing any coding?
A variable declared Public can be accessed either codes inside userform and moduels?
Thanks for your time!
Final
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You are missing UserForm1 in front of Conrtrols.
 
Upvote 0

Forum statistics

Threads
1,215,566
Messages
6,125,597
Members
449,238
Latest member
wcbyers

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