Avatar billede micaud Mester
10. august 2006 - 19:30 Der er 7 kommentarer og
1 løsning

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.
Avatar billede kabbak Professor
10. august 2006 - 19:50 #1
Dine knapkoder

Private Sub CheckBox1_Click()
    Call Skift(CheckBox1)
End Sub

Private Sub CheckBox2_Click()
    Call Skift(CheckBox2)
End Sub
Private Sub CheckBox3_Click()
    Call Skift(CheckBox3)
End Sub

Private Sub OptionButton1_Click()
    Call Skift(OptionButton1)
End Sub

Private Sub OptionButton2_Click()
    Call Skift(OptionButton2)
End Sub



I et modul

Public Sub Skift(CB As Object)
    With CB
        If CB = True Then
            .ForeColor = RGB(0, 0, 205)
            .Font.Bold = True
            .Font = "times new roman"
        Else
            .ForeColor = RGB(135, 206, 235)
            .Font.Bold = False
        End If
    End With
End Sub
Avatar billede micaud Mester
10. august 2006 - 20:38 #2
Perfekt... tak for det.. Nu du er igang kan du så også optimere dette:

Det kan vel også laves nemmere og mere optimalt, da jeg jo skal have 9 forskellige optionsbuttons...

Koden vælger afdelinger i en pivottabel.

If OptionButton1 = True Then
    With ActiveSheet.PivotTables("Pivottabel1").PivotFields("Butik")
        .PivotItems("").Visible = True
        .PivotItems("10").Visible = True
        .PivotItems("20").Visible = True
        .PivotItems("30").Visible = True
        .PivotItems("40").Visible = True
        .PivotItems("42").Visible = True
        .PivotItems("50").Visible = True
        .PivotItems("51").Visible = True
        .PivotItems("59").Visible = True
        .PivotItems("72").Visible = True
        .PivotItems("74").Visible = True
        .PivotItems("81").Visible = True
        .PivotItems("85").Visible = True
        .PivotItems("90").Visible = True
    End With
End If

Smid som svar, så du kan få point....
Avatar billede kabbak Professor
10. august 2006 - 21:30 #3
er det de samme som skal være synlig, ved alle OptionButton ??
Avatar billede micaud Mester
10. august 2006 - 21:31 #4
Nej.... ved optionsbutton2 skal kun "10" være synlig og ved optionsbutton3 kun "20" osv......
Avatar billede micaud Mester
10. august 2006 - 21:37 #5
Kan man evt. definere en variabel for pivotitems og en for optionsbutton, så det hele kan køre i et modul som der calles fra knappen, ligesom den anden du lavede ?
Avatar billede kabbak Professor
10. august 2006 - 21:47 #6
Private Sub OptionButton1_Click()
    Call Skift(OptionButton1)
    Call OPB(OptionButton1, 10)
End Sub

Private Sub OptionButton2_Click()
    Call Skift(OptionButton2)
    Call OPB(OptionButton2, 20)
End Sub


Public Sub OPB(OP As Object, NR As String)
If OP = True Then
    With ActiveSheet.PivotTables("Pivottabel1").PivotFields("Butik")
        .PivotItems("").Visible = False
        .PivotItems("10").Visible = False
        .PivotItems("20").Visible = False
        .PivotItems("30").Visible = False
        .PivotItems("40").Visible = False
        .PivotItems("42").Visible = False
        .PivotItems("50").Visible = False
        .PivotItems("51").Visible = False
        .PivotItems("59").Visible = False
        .PivotItems("72").Visible = False
        .PivotItems("74").Visible = False
        .PivotItems("81").Visible = False
        .PivotItems("85").Visible = False
        .PivotItems("90").Visible = False
        .PivotItems(NR).Visible = True
    End With
End If

End Sub


ER IKKE TESTET
Avatar billede micaud Mester
10. august 2006 - 21:58 #7
Mhhhh virker desværre ikke......
Avatar billede micaud Mester
10. august 2006 - 22:57 #8
Lidt Google søgning hjalp til at finde den endelig løsning.....

If OP = True Then
    With ActiveSheet.PivotTables("Pivottabel1").PivotFields("Butik")
        For X = 1 To .PivotItems.Count
            NR1 = .PivotItems(X).Name
            If NR1 = NR Then
                .PivotItems(X).Visible = True
    Else
                .PivotItems(X).Visible = False
    End If
        Next
    End With
End If
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Excel kurser for alle niveauer og behov – find det kursus, der passer til dig

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester