XL 2016 Faire un Private sub unique pour plusieurs textbox

Goose

XLDnaute Occasionnel
Bjr, j'aimerai savoir si l'on peut faire un code unique pour toutes mes textbox, car j'en ai 15 a configurer ?

VB:
Private Sub Joueur1_Change()
    Joueur1 = UCase(Mid(Joueur1, 1, InStr(1, Joueur1, " "))) & _
        Application.Proper(Mid(Joueur1, InStr(1, Joueur1, " ") + 1))
End Sub
Private Sub Joueur2_Change()
    Joueur2 = UCase(Mid(Joueur2, 1, InStr(1, Joueur2, " "))) & _
        Application.Proper(Mid(Joueur2, InStr(1, Joueur2, " ") + 1))
End Sub
Private Sub Joueur3_Change()
    Joueur3 = UCase(Mid(Joueur3, 1, InStr(1, Joueur3, " "))) & _
        Application.Proper(Mid(Joueur3, InStr(1, Joueur3, " ") + 1))
End Sub
Private Sub Joueur4_Change()
    Joueur4 = UCase(Mid(Joueur4, 1, InStr(1, Joueur4, " "))) & _
        Application.Proper(Mid(Joueur4, InStr(1, Joueur4, " ") + 1))
End Sub
 

piga25

XLDnaute Barbatruc
Bonjour,
Peut être comme cela:
VB:
Private Sub Joueur_Change()
    Dim joueur As Object
    Set joueur = Me.ActiveControl
    
    If TypeName(joueur) = "TextBox" Then
        Dim joueurText As String
        joueurText = joueur.Text
        
        joueurText = UCase(Mid(joueurText, 1, InStr(1, joueurText, " "))) & _
            Application.Proper(Mid(joueurText, InStr(1, joueurText, " ") + 1))
        
        joueur.Text = joueurText
    End If
End Sub
 

piga25

XLDnaute Barbatruc
Re,
J'aurai dû préciser pour chaque TextBox (Joueur1, Joueur2, Joueur3, Joueur4), assurez-vous que l'événement Change est lié à Joueur_Change
VB:
Private Sub Joueur1_Change()
    Call Joueur_Change
End Sub

Private Sub Joueur2_Change()
    Call Joueur_Change
End Sub

Private Sub Joueur3_Change()
    Call Joueur_Change
End Sub

Private Sub Joueur4_Change()
    Call Joueur_Change
End Sub
 
Dernière édition:

piga25

XLDnaute Barbatruc
Re,
Un peu plus court:
VB:
Private Sub FormatJoueur(ByRef joueur As String)
    joueur = UCase(Mid(joueur, 1, InStr(1, joueur, " "))) & _
        Application.Proper(Mid(joueur, InStr(1, joueur, " ") + 1))
End Sub

Private Sub Joueur1_Change()
    FormatJoueur Joueur1
End Sub

Private Sub Joueur2_Change()
    FormatJoueur Joueur2
End Sub

Private Sub Joueur3_Change()
    FormatJoueur Joueur3
End Sub

Private Sub Joueur4_Change()
    FormatJoueur Joueur4
End Sub

Bien sur attribuer à chaque textbox
 

Discussions similaires

Réponses
3
Affichages
1 K

Statistiques des forums

Discussions
312 964
Messages
2 094 010
Membres
105 912
dernier inscrit
willou3869