ways to shorten some vbe text

DB73

Board Regular
Joined
Jun 7, 2022
Messages
102
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2010
  6. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
how do i write the following in a shorter way, if possible.
i would like to have the possibility to change the subjects
VBA Code:
If ComboBox1.value = "ziek" Or ComboBox1.value = "verlof" Or ComboBox1.value = "feestdag" Or ComboBox1.value = "bijzonder verlof" Then

the following comes after "then",is there also a way to make it shorter and the possibility to change the subject if necesary.
i mean, just name all the comboboxes that are needed and the the values and the color in one line.
now i write it for eacht combobox
VBA Code:
   'project
        ComboBox2.Enabled = False
        ComboBox2.value = "-"
        ComboBox2.BackColor = RGB(210, 210, 210)
    'werklocatie
        ComboBox3.Enabled = False
        ComboBox3.value = "-"
        ComboBox3.BackColor = RGB(210, 210, 210)
    'normale of overuren
        ComboBox4.Enabled = False
        ComboBox4.value = "-"
        ComboBox4.BackColor = RGB(210, 210, 210)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can't do them in one go, but you can loop:

VBA Code:
Select Case ComboBox1.value
   Case "ziek", "verlof" , "feestdag", "bijzonder verlof"
      Dim n as long
      for n = 2 to 4
        With Controls("ComboBox" & n)
            .Enabled = False
            .value = "-"
            .BackColor = RGB(210, 210, 210)
         End With
       next n
'... other cases as necessary
End Select
 
Upvote 0
You can't do them in one go, but you can loop:

VBA Code:
Select Case ComboBox1.value
   Case "ziek", "verlof" , "feestdag", "bijzonder verlof"
      Dim n as long
      for n = 2 to 4
        With Controls("ComboBox" & n)
            .Enabled = False
            .value = "-"
            .BackColor = RGB(210, 210, 210)
         End With
       next n
'... other cases as necessary
End Select
thanks, i can follow this one, .....in case of another combobox number thats not following up, like cb1,cb2,cb5,cb8..then its not possible this way...isnt it?
 
Upvote 0
You can still use a loop - you just set up an array of names (or number) of the controls first:

VBA Code:
For each v in array("cb1", "cb2", "cb5", "cb8")
with controls(v)
'...
 
Upvote 0
Solution
You can still use a loop - you just set up an array of names (or number) of the controls first:

VBA Code:
For each v in array("cb1", "cb2", "cb5", "cb8")
with controls(v)
'...
ok...many thanks...solved my problem...
 
Upvote 0
Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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