Bonjour chers tous
Dans mon fichier je voudrais faire une combinaison des choix check box et bouton option .
Avec une combinaison multiple pas de problème mais avec le choix unique check box une erreur se produit.
certainement un mauvais code serait à l'origine.
Aidez moi à corriger.
Merci.
Vos lignes d'initialisation sont mal placées. Elles sont exécutée sur click du bouton OK, APRES que la macro test ait été appelée et avant fermeture.
Sortez ces lignes de la sub OKK_Click pour les mettre dans UserForm_Initialize :
VB:
Private Sub OKK_Click() 'bouton enregistrer
Dim msg As String
With Sheets("Feuil1")
.Range("E2") = TextBox1
.Range("E2") = Trim(.Range("E2"))
End With
Unload InfosC
End Sub
Private Sub UserForm_Initialize()
Dim ctrl As Control
With Sheets("Feuil1")
.Range("E2") = ""
End With
'
' Initialisation des controls
For Each ctrl In Me.Controls
If TypeName(ctrl) Like "CheckBox" Or TypeName(ctrl) Like "OptionButton" Then ctrl = False
Next ctrl
TextBox1 = ""
End Sub
Private Sub OKK_Click() 'bouton enregistrer
Sheets("Feuil1").Range("E2") = TextBox1
Unload InfosC
End Sub
En effet les contrôles reprennent automatiquement leurs valeurs initiales à la fermeture de l'UserForm.
VB:
Sub test()
Dim ctrl As Control
Dim msg As String
For Each ctrl In InfosC.Controls
If TypeName(ctrl) Like "CheckBox" Or TypeName(ctrl) Like "OptionButton" Then
If ctrl = True Then msg = msg & " - " & ctrl.Caption
End If
Next ctrl
InfosC.TextBox1 = Mid(msg, 4)
End Sub
Private Sub OKK_Click() 'bouton enregistrer
Sheets("Feuil1").Range("E2") = TextBox1
Unload InfosC
End Sub
En effet les contrôles reprennent automatiquement leurs valeurs initiales à la fermeture de l'UserForm.
VB:
Sub test()
Dim ctrl As Control
Dim msg As String
For Each ctrl In InfosC.Controls
If TypeName(ctrl) Like "CheckBox" Or TypeName(ctrl) Like "OptionButton" Then
If ctrl = True Then msg = msg & " - " & ctrl.Caption
End If
Next ctrl
InfosC.TextBox1 = Mid(msg, 4)
End Sub
Private Sub OKK_Click() 'bouton enregistrer
Sheets("Feuil1").Range("E2") = TextBox1
Unload InfosC
End Sub
En effet les contrôles reprennent automatiquement leurs valeurs initiales à la fermeture de l'UserForm.
VB:
Sub test()
Dim ctrl As Control
Dim msg As String
For Each ctrl In InfosC.Controls
If TypeName(ctrl) Like "CheckBox" Or TypeName(ctrl) Like "OptionButton" Then
If ctrl = True Then msg = msg & " - " & ctrl.Caption
End If
Next ctrl
InfosC.TextBox1 = Mid(msg, 4)
End Sub
Vos lignes d'initialisation sont mal placées. Elles sont exécutée sur click du bouton OK, APRES que la macro test ait été appelée et avant fermeture.
Sortez ces lignes de la sub OKK_Click pour les mettre dans UserForm_Initialize :
VB:
Private Sub OKK_Click() 'bouton enregistrer
Dim msg As String
With Sheets("Feuil1")
.Range("E2") = TextBox1
.Range("E2") = Trim(.Range("E2"))
End With
Unload InfosC
End Sub
Private Sub UserForm_Initialize()
Dim ctrl As Control
With Sheets("Feuil1")
.Range("E2") = ""
End With
'
' Initialisation des controls
For Each ctrl In Me.Controls
If TypeName(ctrl) Like "CheckBox" Or TypeName(ctrl) Like "OptionButton" Then ctrl = False
Next ctrl
TextBox1 = ""
End Sub