controlbox - Visual basic
Jeg har et regneark, hvori jeg har indsat kontrolelementer direkte i regnearket. 17 checkboxe og 9 optionsbuttons. I visual basic definer jeg makroen sub farve() som vist ovenfor.Alle checkboxe og optionsbuttons ligger som private sub under selve arket og ikke i et modul. Altså 17 stk. Private Sub CheckBoxXX_Click() og 9 stk. Private Sub OptionButtonX_Click().
Når en checkbox markeres skal den og nogle andre have én farve og andre en anden farve. Optionsbutton: Der skal den valgte knap have én farve og resten en anden farve.
Jeg bruger ikke en userform.
Se min makro nedenfor som virker, men jo fylder meget og sikkert ikke optimal.
Håber I kan lære mig lidt optimering i mine makroer.
Det virker desværre ikke. Det hele kører ikke i et modul men under arket selv.
Min endelige sub ser således ud og virker, men fylder jo en krig:
Sub farve()
If CheckBox1 = True Then
CheckBox1.ForeColor = RGB(0, 0, 205)
CheckBox1.Font.Bold = True
CheckBox1.Font = "times new roman"
Else
CheckBox1.ForeColor = RGB(135, 206, 235)
CheckBox1.Font.Bold = False
End If
If CheckBox2 = True Then
CheckBox2.ForeColor = RGB(0, 0, 205)
CheckBox2.Font.Bold = True
CheckBox2.Font = "times new roman"
Else
CheckBox2.ForeColor = RGB(135, 206, 235)
CheckBox2.Font.Bold = False
End If
If CheckBox3 = True Then
CheckBox3.ForeColor = RGB(0, 0, 205)
CheckBox3.Font.Bold = True
CheckBox3.Font = "times new roman"
Else
CheckBox3.ForeColor = RGB(135, 206, 235)
CheckBox3.Font.Bold = False
End If
If CheckBox4 = True Then
CheckBox4.ForeColor = RGB(0, 0, 205)
CheckBox4.Font.Bold = True
CheckBox4.Font = "times new roman"
Else
CheckBox4.ForeColor = RGB(135, 206, 235)
CheckBox4.Font.Bold = False
End If
If CheckBox5 = True Then
CheckBox5.ForeColor = RGB(0, 0, 205)
CheckBox5.Font.Bold = True
CheckBox5.Font = "times new roman"
Else
CheckBox5.ForeColor = RGB(135, 206, 235)
CheckBox5.Font.Bold = False
End If
If CheckBox6 = True Then
CheckBox6.ForeColor = RGB(0, 0, 205)
CheckBox6.Font.Bold = True
CheckBox6.Font = "times new roman"
Else
CheckBox6.ForeColor = RGB(135, 206, 235)
CheckBox6.Font.Bold = False
End If
If CheckBox7 = True Then
CheckBox7.ForeColor = RGB(0, 0, 205)
CheckBox7.Font.Bold = True
CheckBox7.Font = "times new roman"
Else
CheckBox7.ForeColor = RGB(135, 206, 235)
CheckBox7.Font.Bold = False
End If
If CheckBox8 = True Then
CheckBox8.ForeColor = RGB(0, 0, 205)
CheckBox8.Font.Bold = True
CheckBox8.Font = "times new roman"
Else
CheckBox8.ForeColor = RGB(135, 206, 235)
CheckBox8.Font.Bold = False
End If
If CheckBox9 = True Then
CheckBox9.ForeColor = RGB(0, 0, 205)
CheckBox9.Font.Bold = True
CheckBox9.Font = "times new roman"
Else
CheckBox9.ForeColor = RGB(135, 206, 235)
CheckBox9.Font.Bold = False
End If
If CheckBox10 = True Then
CheckBox10.ForeColor = RGB(0, 0, 205)
CheckBox10.Font.Bold = True
CheckBox10.Font = "times new roman"
Else
CheckBox10.ForeColor = RGB(135, 206, 235)
CheckBox10.Font.Bold = False
End If
If CheckBox11 = True Then
CheckBox11.ForeColor = RGB(0, 0, 205)
CheckBox11.Font.Bold = True
CheckBox11.Font = "times new roman"
Else
CheckBox11.ForeColor = RGB(135, 206, 235)
CheckBox11.Font.Bold = False
End If
If CheckBox12 = True Then
CheckBox12.ForeColor = RGB(0, 0, 205)
CheckBox12.Font.Bold = True
CheckBox12.Font = "times new roman"
Else
CheckBox12.ForeColor = RGB(135, 206, 235)
CheckBox12.Font.Bold = False
End If
If CheckBox13 = True Then
CheckBox13.ForeColor = RGB(0, 0, 205)
CheckBox13.Font.Bold = True
CheckBox13.Font = "times new roman"
Else
CheckBox13.ForeColor = RGB(135, 206, 235)
CheckBox13.Font.Bold = False
End If
If CheckBox14 = True Then
CheckBox14.ForeColor = RGB(0, 0, 205)
CheckBox14.Font.Bold = True
CheckBox14.Font = "times new roman"
Else
CheckBox14.ForeColor = RGB(135, 206, 235)
CheckBox14.Font.Bold = False
End If
If CheckBox15 = True Then
CheckBox15.ForeColor = RGB(0, 0, 205)
CheckBox15.Font.Bold = True
CheckBox15.Font = "times new roman"
Else
CheckBox15.ForeColor = RGB(135, 206, 235)
CheckBox15.Font.Bold = False
End If
If CheckBox16 = True Then
CheckBox16.ForeColor = RGB(0, 0, 205)
CheckBox16.Font.Bold = True
CheckBox16.Font = "times new roman"
Else
CheckBox16.ForeColor = RGB(135, 206, 235)
CheckBox16.Font.Bold = False
End If
If CheckBox17 = True Then
CheckBox17.ForeColor = RGB(0, 0, 205)
CheckBox17.Font.Bold = True
CheckBox17.Font = "times new roman"
Else
CheckBox17.ForeColor = RGB(135, 206, 235)
CheckBox17.Font.Bold = False
End If
If OptionButton1 = True Then
OptionButton1.ForeColor = RGB(0, 0, 205)
OptionButton1.Font.Bold = True
OptionButton1.Font = "times new roman"
Else
OptionButton1.ForeColor = RGB(135, 206, 235)
OptionButton1.Font.Bold = False
End If
If OptionButton2 = True Then
OptionButton2.ForeColor = RGB(0, 0, 205)
OptionButton2.Font.Bold = True
OptionButton2.Font = "times new roman"
Else
OptionButton2.ForeColor = RGB(135, 206, 235)
OptionButton2.Font.Bold = False
End If
If OptionButton3 = True Then
OptionButton3.ForeColor = RGB(0, 0, 205)
OptionButton3.Font.Bold = True
OptionButton3.Font = "times new roman"
Else
OptionButton3.ForeColor = RGB(135, 206, 235)
OptionButton3.Font.Bold = False
End If
If OptionButton4 = True Then
OptionButton4.ForeColor = RGB(0, 0, 205)
OptionButton4.Font.Bold = True
OptionButton4.Font = "times new roman"
Else
OptionButton4.ForeColor = RGB(135, 206, 235)
OptionButton4.Font.Bold = False
End If
If OptionButton5 = True Then
OptionButton5.ForeColor = RGB(0, 0, 205)
OptionButton5.Font.Bold = True
OptionButton5.Font = "times new roman"
Else
OptionButton5.ForeColor = RGB(135, 206, 235)
OptionButton5.Font.Bold = False
End If
If OptionButton6 = True Then
OptionButton6.ForeColor = RGB(0, 0, 205)
OptionButton6.Font.Bold = True
OptionButton6.Font = "times new roman"
Else
OptionButton6.ForeColor = RGB(135, 206, 235)
OptionButton6.Font.Bold = False
End If
If OptionButton7 = True Then
OptionButton7.ForeColor = RGB(0, 0, 205)
OptionButton7.Font.Bold = True
OptionButton7.Font = "times new roman"
Else
OptionButton7.ForeColor = RGB(135, 206, 235)
OptionButton7.Font.Bold = False
End If
If OptionButton8 = True Then
OptionButton8.ForeColor = RGB(0, 0, 205)
OptionButton8.Font.Bold = True
OptionButton8.Font = "times new roman"
Else
OptionButton8.ForeColor = RGB(135, 206, 235)
OptionButton8.Font.Bold = False
End If
If OptionButton9 = True Then
OptionButton9.ForeColor = RGB(0, 0, 205)
OptionButton9.Font.Bold = True
OptionButton9.Font = "times new roman"
Else
OptionButton9.ForeColor = RGB(135, 206, 235)
OptionButton9.Font.Bold = False
End If
End Sub
Jeg vil blot have checkboxen til at ændre farve, hvis den er checked eller ikke-checked.
Checkboksen kører ikke i en formular, men blot som et kontrolelement i arket.
