Problème de comparaison de chaînes

Bonjour,

j'ai un programme dans lequel je dois comparer la valeur d'une chaîne entrée dans une TextBox avec la valeur d'une cellule (afin de savoir la ligne sur laquelle apporter les modifications).

Je vous met ici le code, suivi de mon fichier pour que vous voyiez bien de quoi je parle.

Public Nom1, Nom2, Nom3, Nom4, Nom5, Nom6, Nom7, Nom8, Nom9, Nom10, Nom11, Nom12, Nom13, Nom14, Nom15 As String

Private Sub BouttonMiseAJour_Click()
    Dim Ligne As String
    Nom1 = LCase$(Cells(17, "A"))
    Nom2 = LCase$(Cells(18, "A"))
    Nom3 = LCase$(Cells(19, "A"))
    Nom4 = LCase$(Cells(20, "A"))
    Nom5 = LCase$(Cells(21, "A"))
    Nom6 = LCase$(Cells(22, "A"))
    Nom7 = LCase$(Cells(23, "A"))
    Nom8 = LCase$(Cells(24, "A"))
    Nom9 = LCase$(Cells(25, "A"))
    Nom10 = LCase$(Cells(26, "A"))
    Nom11 = LCase$(Cells(27, "A"))
    Nom12 = LCase$(Cells(28, "A"))
    Nom13 = LCase$(Cells(29, "A"))
    Nom14 = LCase$(Cells(30, "A"))
    Nom15 = LCase$(Cells(31, "A"))
    If TextBox1.Text = "" Or TextBox2.Text = "" Then
        MsgBox "Veuillez remplir tous les champs, merci."
    Else
        Ligne = TextBox1.Text
        If StrComp(Ligne, Nom1, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom2, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom3, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom4, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom5, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom6, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom7, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom8, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom9, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom10, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom11, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom12, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom13, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom14, vbTextCompare) = 0 Then
                MsgBox "Ok"
            ElseIf StrComp(Ligne, Nom15, vbTextCompare) = 0 Then
                MsgBox "Ok"
            Else
                MsgBox "Veuillez entrer un nom de conducteur correct, merci."
        End If
    End If
End Sub

Private Sub BouttonFermer_Click()
    Unload Me
End Sub

Private Sub BouttonParcourir_Click()
    Dim Adresse As String, xString As String
    Adresse = Application.GetOpenFilename
    On Error Resume Next
    If Err.Number <> 0 Then Exit Sub
    xString = Mid(Adresse, 2 + Len(Adresse) - InStr(StrReverse(Adresse), "\"))
    xString = Left(xString, InStr(xString, ".") - 1)
    TextBox2 = xString
End Sub

Private Sub Formulaire2_Initialize()
    Formulaire.Height = 136
    Formulaire.Width = 311
End Sub

Je précise que j'ai essayé plusieurs autres méthodes, de la plus simple à la plus étranges, mas rien n'y fait, alors que les valeurs semblent les mêmes, la condition n'est jamais validée, et je n'arrive pas à savoir pourquoi.

Merci d'avance pour vos réponses

Cordialement

Kazel

26g-04-2013.xlsm (76.63 Ko)

Bonjour,

La fonction StrComp renvoie 0 si les 2 chaînes sont égales.

En A17, tu as placé 2 espaces entre BACHELET et FRANCK.

Si tu compares avec le texte "BACHELET FRANCK" (un seul espace entre nom et prénom), StrComp renvoie 1.

A+

Ah bah niquel merci, j'ai toujours cherché l'erreur dans le code et non sur le fichier en lui même,du coup j'ai une soudaine envie de chanter Trollsong pour ma stupidité ^^'.

En tout cas merci à toi pour ta réponse, je commençais vraiment à désespérer de ne jamais y arriver.

Cordialement

Kazel

Rechercher des sujets similaires à "probleme comparaison chaines"