Griser un bouton si les champs sont vides VBA
Bonjour à tous,
Voila j'ai un formulaire avec 3 textbox intitulé textbox1, Textbox2, Textbox3 et une ListBox nomé ComboBox1.
Je voudrais que si un champ n'est pas rempli que le bouton reste grisaille afin que l'utilisateur ne puisse pas valider la sélection.
Ci joint le fichier.
Merci par avance à tous pour votre aide,
- Messages
- 2'417
- Excel
- 2019
- Inscrit
- 13/07/2017
- Emploi
- Formateur, animateur,tech.informatique
Bonjour toutes et tous
@tester et à améliorer
remerciement à MFerrand, un bout de code pour le' BoutonActif'
Note: j'aurai voulu faire avec les Tag de Contrôle mais, je ne suis pas arrivé
Info: si un des champs des 3 textboxs non remplis le bouton est grisé, si léger bug revenir sur une textbox est le bouton redevient cliquable
j'pense qu'il doit y avoir encore plus simple mais en attendant ci-joint
- 3 TextBox + 4 Labels + 1 ComboBox + 2 boutons de commande (Valider et sortir)
Private Sub ComboBox1_Enter()
If Me.TextBox1.Value <> "" And Me.TextBox2.Value <> "" And Me.TextBox3.Value <> "" Then
Me.CommandButton1.Enabled = True
Else
Me.CommandButton1.Enabled = False
End If
End Sub
Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox1.Value <> "" And Me.TextBox2.Value <> "" And Me.TextBox3.Value <> "" Then
Me.CommandButton1.Enabled = True
Else
Me.CommandButton1.Enabled = False
End If
End Sub
Private Sub CommandButton1_Click()
On Error GoTo 0
If Me.TextBox1.Value <> "" And Me.TextBox2.Value <> "" And Me.TextBox3.Value <> "" Then
Range("B6").Value = TextBox1
Range("B7").Value = TextBox2
Range("B8").Value = TextBox3
Range("B9").Value = ComboBox1
Unload Me
Else
MsgBox "merci de remplir tout les champs"
Me.CommandButton1.Enabled = False
End If
End Sub
Private Sub CommandButton2_Click()
' on décharge l'usf1
Unload Me
End Sub
Private Sub ComboBox1_AfterUpdate()
BoutonActif
End Sub
Private Sub TextBox1_Change()
' 1ere lettre en Majuscule pour textbox Marque
Dim sta As String
sta = TextBox1.Text
TextBox1.Text = UCase(Mid(sta, 1, 1)) & Mid$(sta, 2, Len(sta))
TextBox1.SelStart = Len(sta)
End Sub
Private Sub TextBox3_Change()
' 1ere lettre en Majuscule pour textbox Pays
Dim st As String
st = TextBox3.Text
TextBox3.Text = UCase(Mid(st, 1, 1)) & Mid$(st, 2, Len(st))
TextBox3.SelStart = Len(st)
End Sub
Private Sub UserForm_Initialize()
' on rend grisé le bouton
Me.CommandButton1.Enabled = False
End Sub
Sub BoutonActif()
Dim i%, j%
For i = 1 To 3
If Controls("TextBox" & i).Value = "" Then
CommandButton1.Enabled = False
Exit Sub
End If
Next i
CommandButton1.Enabled = True
End Subcrdlt,
André
Super merci pour votre prompte retour