Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1912to1916
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Optionbutton Farbe ändern
03.01.2023 19:28:33
walli
Guten Abend,
ich habe folgendes im Makro stehen:
If OptionButton1.Value = True Then ActiveSheet.Cells(13, 11) = "Frau"
OptionButton1.BackColor = &HFF& 'rot
das ist soweit i.o. aber
wenn in der Zelle eine andere Anrede steht, außer Frau,
soll das Optionsbutton grün werden &HC000&
Ich habe 4 Optionbutton.
mfg walli

15
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Optionbutton Farbe ändern
03.01.2023 19:31:25
onur

If OptionButton1 Then
ActiveSheet.Cells(13, 11) = "Frau"
OptionButton1.BackColor = &HFF& 'rot
Else
OptionButton1.BackColor = rgb(0,255,0)
End If

Dank, habe so eingesetzt -)
03.01.2023 19:40:18
walli
Hallo,
so klappt es
Private Sub OptionButton2_Click()
OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton2.BackColor = &HFF&
OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If OptionButton2.Value = True Then ActiveSheet.Cells(13, 11) = "Herrn "
OptionButton2.Value = False
mfg walli
Anzeige
Hallo onur, muss doch mal...
03.01.2023 19:55:27
walli
Hallo Onur,
muss doch mal Fragen:
Ich kopiere Daten in diese Tabelle, wenn dann eine andere Anrede steht außer "Frau", könnte man die OptionButton Farbe auch
ändern ?
Hier sind die Makros:

Private Sub OptionButton1_Click()
OptionButton1.BackColor = &HFF&             ' rot
OptionButton2.BackColor = &HC0FFC0
OptionButton3.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
OptionButton4.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
If OptionButton1.Value = True Then ActiveSheet.Cells(13, 11) = "Frau"
OptionButton1.Value = False
End Sub

Private Sub OptionButton2_Click()
OptionButton1.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
OptionButton2.BackColor = &HFF&             'rot
OptionButton3.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
OptionButton4.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
If OptionButton2.Value = True Then ActiveSheet.Cells(13, 11) = "Herrn"
OptionButton2.Value = False
End Sub

Private Sub OptionButton3_Click()
OptionButton3.BackColor = &HFF&         ' rot
OptionButton1.BackColor = &HC0FFC0
OptionButton2.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
OptionButton4.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
If OptionButton3.Value = True Then ActiveSheet.Cells(13, 11) = "Eheleute"
OptionButton3.Value = False
End Sub

Private Sub OptionButton4_Click()
OptionButton4.BackColor = &HFF&         'rot
OptionButton1.BackColor = &HC0FFC0
OptionButton2.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
OptionButton3.BackColor = &HC0FFC0        ' dunkelgrün RGB(0, 255, 0)
If OptionButton4.Value = True Then ActiveSheet.Cells(13, 11) = "Firma"
OptionButton4.Value = False
End Sub
mfg walli
Anzeige
AW: Hallo onur, muss doch mal...
03.01.2023 20:09:26
onur

Private Sub Worksheet_Change(ByVal Target As Range)
OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If ActiveSheet.Cells(13, 11) = "Frau" Then OptionButton1.BackColor = &HFF&
End Sub

AW: Optionbutton Farbe ändern
03.01.2023 19:32:32
walli
Hier alle makros:

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then ActiveSheet.Cells(13, 11) = "Frau"
OptionButton1.Value = False
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then ActiveSheet.Cells(13, 11) = "Herrn "
'  OptionButton1.Value = False
'  OptionButton3.Value = False
'  OptionButton4.Value = False
OptionButton2.Value = False
End Sub

Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then ActiveSheet.Cells(13, 11) = "Eheleute"
'  OptionButton1.Value = False
'  OptionButton2.Value = False
'  OptionButton4.Value = False
OptionButton3.Value = False
End Sub

Private Sub OptionButton4_Click()
If OptionButton4.Value = True Then ActiveSheet.Cells(13, 11) = "Firma"
'  OptionButton1.Value = False
'  OptionButton2.Value = False
'  OptionButton3.Value = False
OptionButton4.Value = False
End Sub
gruß walli
Anzeige
AW: Optionbutton Farbe ändern
04.01.2023 09:54:09
Daniel
Hi
Du müsstest dann die drei anderen Optionbuttons grün färben, weil du nicht weißt, welcher zuvor rot war und das bei allen vier Buttons.
Ich würde hier anstelle der Optionbuttons eine Listbox verwenden, dann muss nan gar nichts programmieren.
Danke, das reicht so ! -)
04.01.2023 11:04:23
walli
Doch ein Fehler...
04.01.2023 12:02:33
walli
Hallo zusammen,
ich habe folgendes eingesetzt:
Public Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.Cells(13, 11) = "Frau" Then OptionButton1.BackColor = &HFF&
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If ActiveSheet.Cells(13, 11) = "Herrn" Then OptionButton2.BackColor = &HFF&
' OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If ActiveSheet.Cells(13, 11) = "Eheleute" Then OptionButton3.BackColor = &HFF&
' OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If ActiveSheet.Cells(13, 11) = "Familie" Then OptionButton4.BackColor = &HFF&
' OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If ActiveSheet.Cells(13, 11) = "An" Then OptionButton5.BackColor = &HFF&
' OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
If ActiveSheet.Cells(13, 11) = "Firma" Then OptionButton6.BackColor = &HFF&
' OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
Das Problem:
Wenn die Anrede in die Zelle kopiert wird, werden die anderen OptionButton nicht grün gefärbt.
Habe mal auskommentiert, entsprechend der Auswahl wird ROT gefärbt.
wo ist der Fehler ?
mfg walli
Anzeige
AW: Doch ein Fehler...
04.01.2023 12:06:53
onur

If das und das then
Tu dies
Tu das
Tu jenes
end if
Wenn ALLES gemacht werden soll, wenn die Bedingung zutrifft !
Kauf dir mal ein VBA-Buch.
Hallo onur, witzig...
04.01.2023 12:14:16
walli
Hallo onur,
habe dich bisher als Fachmann sehr geschätzt, deshalb verstehe ich deinen Kommentar nicht !
Ich habe alle auskommentierten drin gehabt, nur die Anrede "An" klappte.
mfg walli
AW: Hallo onur, witzig...
04.01.2023 12:18:28
onur
Nochmal für die "TextNichtLeserSondernNurÜberflieger":
Nicht SO:

If ActiveSheet.Cells(13, 11) = "Frau" Then OptionButton1.BackColor = &HFF&
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
Sondern SO:

If ActiveSheet.Cells(13, 11) = "Frau" Then
OptionButton1.BackColor = &HFF&
' OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
' OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
End If

Anzeige
AW: Hallo onur, witzig...
04.01.2023 12:42:01
onur
Wenn du mehrere Aktionen durchführen willst, wenn eine Bedingung zutrifft, müssen ALLE diese Aktionen zwischen "If" und "End If" stehen. So wie du das gemacht hast, wird nur die Aktion direkt hinter Then bedingt durchgeführt. Die Anderen darunter werden bedingungslos immer durchgeführt. Das "IF" ist wie eine "Klammer auf" und das "End If" ist wie ein "Klammer zu", das einschliesst, was alles gemacht werden soll, wenn die Bedingung zutrifft.
Wenn nur eine einzige Aktion vorhanden ist, reicht

If ... Then TuDies

AW: Hallo onur, witzig...
04.01.2023 12:25:51
onur
Ausserdem ist es Quatsch, jedesmal den Code zu wiederholen, der alle anderen Optionbuttons grün färbt.
So ist das ganze WESENTLICH kompakter:

Public Sub Worksheet_Change(ByVal Target As Range)
'ALLE Optionbuttons grün färben
OptionButton1.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton2.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton3.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton4.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton5.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
OptionButton6.BackColor = &HC0FFC0 ' dunkelgrün RGB(0, 255, 0)
'Entsprechendes rot färben
If ActiveSheet.Cells(13, 11) = "Frau" Then OptionButton1.BackColor = &HFF&
If ActiveSheet.Cells(13, 11) = "Herrn" Then OptionButton2.BackColor = &HFF&
If ActiveSheet.Cells(13, 11) = "Eheleute" Then OptionButton3.BackColor = &HFF&
If ActiveSheet.Cells(13, 11) = "Familie" Then OptionButton4.BackColor = &HFF&
If ActiveSheet.Cells(13, 11) = "An" Then OptionButton5.BackColor = &HFF&
If ActiveSheet.Cells(13, 11) = "Firma" Then OptionButton6.BackColor = &HFF&
End Sub

Anzeige
Herzlichen Dank ! -)
04.01.2023 12:45:10
walli
Hallo onur,
herzlichen Dank !
mfg walli
Gerne !
04.01.2023 12:46:26
onur
Hoffe, du hast was gelernt.

190 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige