Loop thru user form controls

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have four option buttons on my user form and need to enable/disable some text boxes based on which option is selected. The code below works fine, but before I code the same for the other there option buttons, can this be reconstructed into the loop thru?

Code:
Private Sub Opt1_Click()
    If Me.Opt1 Then
        Me.txtQtr1.Enabled = True
        Me.txtQtr1e.Enabled = True
        Me.txtQtr1n.Enabled = True
        
        Me.txtQtr2.Enabled = False
        Me.txtQtr2e.Enabled = False
        Me.txtQtr2n.Enabled = False
        
        Me.txtQtr3.Enabled = False
        Me.txtQtr3e.Enabled = False
        Me.txtQtr3n.Enabled = False
        
        Me.txtQtr4.Enabled = False
        Me.txtQtr4e.Enabled = False
        Me.txtQtr4n.Enabled = False
    End If
End Sub
 
Can quite get the colours right, think I might have the logic the wrong way round too but give this a shot.
Code:
Sub EnableQuarter(lngQtr As Long)
Dim I As Long

    For I = 1 To 4
    
        With Me.Controls("txtQtr" & I)
            .BackColor = IIf(lngQtr = I, vbWhite, vbBlue)
            .Enabled = lngQtr = I
        End With
        
        With Me.Controls("txtQtr" & I & "e")
            .BackColor = IIf(lngQtr = I, vbWhite, vbBlue)
            .Enabled = lngQtr = I
        End With

        With Me.Controls("txtQtr" & I & "n")
            .BackColor = IIf(lngQtr = I, vbWhite, vbBlue)
            .Enabled = lngQtr = I
        End With
    Next I

End Sub
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Thanks Norie, this does the job. Appreciate your time.
 
Upvote 0

Forum statistics

Threads
1,215,193
Messages
6,123,560
Members
449,108
Latest member
rache47

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