simplification code option button

zefirstfan

XLDnaute Junior
Bonjour,
Le code ci dessous fonctionne très bien mais je pense qu'il est possible de le simplifier (boucle???)
Les Option button sont dans le même frame.

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
Label1 = "1"
End If
End Sub

Private Sub OptionButton2_Click()
If OptionButton2 = True Then
Label1 = "2"
End If
End Sub

Private Sub OptionButton3_Click()
If OptionButton3 = True Then
Label1 = "3"
End If
End Sub

J'ai essayé avec des ElseIf mais ça plante.
Une piste?
Merci d'avance.
 

Gaetan

XLDnaute Nouveau
Re : simplification code option button

Voici une syntaxe possible :

Code:
Private Sub OptionButton1_Click()
call maj_textbox
End Sub

Private Sub OptionButton2_Click()
call maj_textbox
End Sub

Private Sub OptionButton3_Click()
call maj_textbox
End Sub

Sub maj_textbox()
If OptionButton1 Then
Label1 = 1
ElseIf OptionButton2 Then
Label1 = 2
ElseIf OptionButton3 Then
Label1 = 3
End If
End Sub

On peut aussi gérer ça avec des modules de classe, mais c'est beaucoup plus complexe.

Cordialement

Gaetan
XLérateur | Accélérez Excel !
 
Dernière édition:

Paf

XLDnaute Barbatruc
Re : simplification code option button

Bonjour à tous

une autre solution guère plus simple
Code:
Private Sub OptionButton1_Click()
TestOptionButton 'appel de la sub 
End Sub

Private Sub OptionButton2_Click()
TestOptionButton 'appel de la sub 
End Sub

Private Sub OptionButton3_Click()
TestOptionButton 'appel de la sub 
End Sub

Sub TestOptionButton()
For i = 1 To 3
    If Controls("OptionButton" & i).Value = True Then
        Label1 = i
        Exit Sub
    End If
Next
End Sub

A+
 
G

Guest

Guest
Re : simplification code option button

Bonjour à tous,

Une proposition:
Code:
Private Sub MAJ_Label()
    Label1 = OptionButton1 * -1 + OptionButton2 * -2 + OptionButton3 * -3
End Sub

Private Sub OptionButton1_Change()
MAJ_Label
End Sub
Private Sub OptionButton2_Change()
MAJ_Label
End Sub
Private Sub OptionButton3_Change()
MAJ_Label
End Sub

A+
 

Si...

XLDnaute Barbatruc
Re : simplification code option button

salut

Bonsour®
:D pour XLDnaute nouveau... mais pas pour un Exceliste antédiluvien...:D

cà vaut le coup Si... le nombre de contrôles est conséquent.
Ici, 3 options interdépendantes, je me contenterais de :
3 macros simples
Code:
Private Sub OptionButton1_Click()
  Label1 = 1
End Sub
Private Sub OptionButton2_Click()
  Label1 = 2
End Sub
Private Sub OptionButton3_Click()
  Label1 = 3
End Sub
ou 1 macro mais 2 clics (Option puis Label)
Code:
Private Sub Label1_Click()
  Dim n As Byte
  For n = 1 To 3
    If Me("Optionbutton" & n) Then Label1 = n
  Next
End Sub
 

laetitia90

XLDnaute Barbatruc
Re : simplification code option button

bonjour tous :):):):):)

on peut utiliser mousemove & tag

suppose optionb...concerne dans frame 1

Code:
Private Sub Frame1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
 If Frame1.ActiveControl.Tag <> "" Then Frame1.Label1 = Frame1.ActiveControl.Tag
End Sub

dans propriéte TAG de chaque optionb...concerne je met une valeur ou autre!!!
 

Discussions similaires

Statistiques des forums

Discussions
312 294
Messages
2 086 953
Membres
103 404
dernier inscrit
sultan87