Macro sélective

Résolu
Bonjour,

Un petit coup de main serait très gentil.
J'ai précédemment récupéré une macro sur ce site qui fonctionne très bien, mais je voudrais savoir si je peux l'assigner juste à 2 colonnes distinctes.

Voici la macro :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim text As String
text = Target.Value
Target.Value = UCase(Left(text, 1)) + Mid(text, 2, Len(text))
End Sub

En remerciant encore une fois OZONE pour son aide sur cette macro.

Je voudrais qu'elle ne concerne que les colonnes D et F d'un classeur.

Possible ou non ?
Cordialement

2 réponses

  1. Bonjour,

    comme cela

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim text As String
    text = Target.Value
    If Target.Column <> 4 And Target.Column <> 6 Then Exit Sub
    Target.Value = UCase(Left(text, 1)) + Mid(text, 2, Len(text))
    End Sub


    1
    1. Super Mike, je peux avancer sur mon fichier maintenant.
      merci
      0
  2. Re,

    Une autre façon de l'écrire

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim text As String
    text = Target.Value
    If Not Application.Intersect(Target, Union(Range("D:D"), Range("F:F"))) Is Nothing Then
    Target.Value = UCase(Left(text, 1)) + Mid(text, 2, Len(text))
    End If
    End Sub 


    ou encore
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim text As String
    text = Target.Value
    If Not Application.Intersect(Target, Union(Columns("D"), Columns("F"))) Is Nothing Then
    Target.Value = UCase(Left(text, 1)) + Mid(text, 2, Len(text))
    End If
    End Sub


    et si tu veux éviter le End If en fin de code et donc une ligne, il suffit d'ajouter un espace et le tiret du 8 après le Then _
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim text As String
    text = Target.Value
    If Not Application.Intersect(Target, Union(Columns("D"), Columns("F"))) Is Nothing Then _
    Target.Value = UCase(Left(text, 1)) + Mid(text, 2, Len(text))

    End Sub

    1
    1. c'est extraordinaire, j'en apprends plein d'un coup, merci.
      0