combine change event on 1 combobox

DB73

Board Regular
Joined
Jun 7, 2022
Messages
107
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2010
  6. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
@Alex Blakenburg gave me today a code here on the forum for running a change event on a userform combobox
but now im tryin to work it out..
the code work perfectly but i need, almost the same code, to run on/in the same combobox
the first code works on CB 2 - 17, at value 1, value2 value3, value4
but the 2nd has to run from 11 - 17, only when another value is choosen in the combobox (value 5)
so i tought to combine these 2 code....but i think it doesnt work on a change event

code 1, from Alex;
VBA Code:
Private Sub ComboBox1_Change()
    Dim i As Long

    Select Case Me.ComboBox1.Value
        Case "ziek", "verlof", "feestdag", "bijzonder verlof"
            For i = 2 To 17
                With Me.Controls("ComboBox" & i)
                   .Enabled = False
                   .Value = "-"
                   .BackColor = RGB(224, 224, 224)
                End With
            Next i
        
        Case Else
          For i = 2 To 17
             With Me.Controls("ComboBox" & i)
                .Enabled = True
                .Value = ""
                .BackColor = RGB(255, 255, 255)
             End With
          Next i
    End Select
End Sub

code 2 (to combine with 1)
VBA Code:
Private Sub ComboBox1_Change()
    Dim i As Long

    Select Case Me.ComboBox1.Value
        Case "thuiswerk"
            For i = 11 To 17
                With Me.Controls("ComboBox" & i)
                   .Enabled = False
                   .Value = "-"
                   .BackColor = RGB(224, 224, 224)
                End With
            Next i
        
        Case Else
          For i = 2 To 17
             With Me.Controls("ComboBox" & i)
                .Enabled = True
                .Value = ""
                .BackColor = RGB(255, 255, 255)
             End With
          Next i
    End Select
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Perhaps this?

VBA Code:
Private Sub ComboBox1_Change()
    Dim i As Long
    
    Select Case Me.ComboBox1.Value
        Case "ziek", "verlof", "feestdag", "bijzonder verlof"
            For i = 2 To 17
                With Me.Controls("ComboBox" & i)
                    .Enabled = False
                    .Value = "-"
                    .BackColor = RGB(224, 224, 224)
                End With
            Next i
            
        Case "thuiswerk"
            For i = 11 To 17
                With Me.Controls("ComboBox" & i)
                    .Enabled = False
                    .Value = "-"
                    .BackColor = RGB(224, 224, 224)
                End With
            Next i
            
        Case Else
            For i = 2 To 17
                With Me.Controls("ComboBox" & i)
                    .Enabled = True
                    .Value = ""
                    .BackColor = RGB(255, 255, 255)
                End With
            Next i
    End Select
End Sub
 
Upvote 0
Solution
Perhaps this?

VBA Code:
Private Sub ComboBox1_Change()
    Dim i As Long
   
    Select Case Me.ComboBox1.Value
        Case "ziek", "verlof", "feestdag", "bijzonder verlof"
            For i = 2 To 17
                With Me.Controls("ComboBox" & i)
                    .Enabled = False
                    .Value = "-"
                    .BackColor = RGB(224, 224, 224)
                End With
            Next i
           
        Case "thuiswerk"
            For i = 11 To 17
                With Me.Controls("ComboBox" & i)
                    .Enabled = False
                    .Value = "-"
                    .BackColor = RGB(224, 224, 224)
                End With
            Next i
           
        Case Else
            For i = 2 To 17
                With Me.Controls("ComboBox" & i)
                    .Enabled = True
                    .Value = ""
                    .BackColor = RGB(255, 255, 255)
                End With
            Next i
    End Select
End Sub
Works perfectly (y)
And now i see what i did wrong...
but can i add as much "case"as i want ?
 
Upvote 0
Works perfectly (y)
And now i see what i did wrong...
but can i add as much "case"as i want ?
Pretty much. But it is a form of hard coding, so if you have a lot of these, it may be worth considering having a mapping table in the workbook.
 
Upvote 0
Pretty much. But it is a form of hard coding, so if you have a lot of these, it may be worth considering having a mapping table in the workbook.
ok...tx for the help.
i try to work ik out now.
im just a beginner...so got a lot to learn
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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