Aide codification VBA - Code Postal canadien

Bonjour,

Je mets en place un fichier avec des formats obligatoires pour chaque colonne de ma feuille Excel. Dans une de mes colonnes, j'ai le code postal canadien : G6R 2B6 (lettre,chiffre,lettre,espace,chiffre,lettre,chiffre). J'ai donc trouvé le code VBA suivant que j'ai appliqué à ma feuille et cela va super bien.

Par contre, lorsque j'entre un code postal et oups que je veux l'enlever, il me dit qu'il y a une erreur et met ma cellule en rouge. Y a t-il moyen, lorsqu'il y a un code postal et que je veux l'enlever que la cellule soit blanche si vide ?

Merci pour votre super aide car je suis pas une pro de VBA

Trinitybarb

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Rg As Range

Set Rg = Intersect(Target, Columns(16))

Application.EnableEvents = False

If Not Rg Is Nothing Then

For Each c In Rg

c.Value = UCase(Application.Trim(c))

If c.Value Like "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" Or _

c.Value Like "[A-Z][0-9][A-Z][0-9][A-Z][0-9­]" Then

c.Value = Left(c, 3) & " " & Right(c, 3)

c.Interior.ColorIndex = xlNone

c.Font.ColorIndex = xlAutomatic

Else

MsgBox "la saisie du code postal est inexacte"

c.Interior.ColorIndex = 3

c.Font.ColorIndex = 2

End If

Next

End If

Application.EnableEvents = True

End Sub

Bonjour et bienvenue sur le forum

Neutralise les 3 instructions qui suivent ''Else'' en mettant une apostrophe devant.

Tu pourras ainsi les remettre si tu as des regrets :

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Rg As Range
Set Rg = Intersect(Target, Columns(16))
Application.EnableEvents = False
If Not Rg Is Nothing Then
    For Each c In Rg
        c.Value = UCase(Application.Trim(c))
        If c.Value Like "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" Or _
            c.Value Like "[A-Z][0-9][A-Z][0-9][A-Z][0-9­]" Then
            c.Value = Left(c, 3) & " " & Right(c, 3)
            c.Interior.ColorIndex = xlNone
            c.Font.ColorIndex = xlAutomatic
        Else
            'MsgBox "la saisie du code postal est inexacte"
            'c.Interior.ColorIndex = 3
            'c/.Font.ColorIndex = 2
        End If
    Next
End If
Application.EnableEvents = True
End Sub

Bye !

Bonjour,

Super merci pour votre aide.

C'est parfait.

Une formule un petit peu plus élaborée :

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Rg As Range
Set Rg = Intersect(Target, Columns(16))
Application.EnableEvents = False
If Not Rg Is Nothing Then
    For Each c In Rg
        c.Value = UCase(Application.Trim(c))
        If c.Value Like "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" Or _
            c.Value Like "[A-Z][0-9][A-Z][0-9][A-Z][0-9­]" Then
            c.Value = Left(c, 3) & " " & Right(c, 3)
            c.Interior.ColorIndex = xlNone
            c.Font.ColorIndex = xlAutomatic
        ElseIf c.Value <> "" Then
            MsgBox "la saisie du code postal est inexacte"
            c.Interior.ColorIndex = 3
            c.Font.ColorIndex = 2
        Else
            c.Interior.ColorIndex = xlNone
            c.Font.ColorIndex = xlAutomatic
        End If
    Next
End If
Application.EnableEvents = True
End Sub

Bye !

super, ça fonctionne parfaitement.

un gros gros merci

Rechercher des sujets similaires à "aide codification vba code postal canadien"