Centrer sur plusieurs cellules sans fusionner

Bonsoir,

voici mon problème. J'ai une macro VBA qui fusionne une sélection et applique un format. Je souhaite l'adapter en centrant la sélection mais sans fusionner. J'ai essayé de remplacer ".HorizontalAlignment = xlCenter" par " .HorizontalAlignment = xlCenterAcrossSelection" mais le résultat reste fusionné. Voici la requête actuelle :

Sub DRILL_2C()

    intitu = InputBox("Intitulé du DRILL")
    If intitu = "" Then End
    test = vide
    If test = True Then
        fusion = MsgBox("Voulez vous fusionner avec l'existant", 4) 'demande si concatenation
        Else: fusion = 7
    End If
    col = Selection.Column
    rang = Selection.Row
    longu = Selection.Count
    vers = sens
    ' fusietcentr Macro
' fusionne et centre les cellules
'

'
    With Selection
        .HorizontalAlignment = xlCenterAcrossSelection
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    For i = 0 To longu - 1
        txt = Cells(rang + i, col).text
        If fusion = 6 And txt <> "" Then Ntxt = "Drill " & txt & Chr(10) & intitu Else Ntxt = "Drill " & intitu
        ' concatenation des données
       Select Case vers
        Case 1: With Cells(rang, col + i)
                                .Value = Ntxt
                                .HorizontalAlignment = xlCenter
                                .Interior.ColorIndex = 3
                End With
        Case 2: With Cells(rang + i, col)
                                .Value = Ntxt
                                .HorizontalAlignment = xlCenter
                                .Interior.ColorIndex = 3
                End With
       End Select
    Next i
End Sub

Salut et bienvenue sur le Forum,

Avec l'enregistreur de macro tu peux créer des codes puis les épurés selon tes besoins.

Voici ci-dessous un code enregistré en centrant un texte sur plusieurs cellules puis ce qu'il faut en garder afin de répondre à ta demande.

Sub Macro1()
'
' Macro1 Macro
'

    Range("C2:G2").Select ' A GARDER
    With Selection
        .HorizontalAlignment = xlCenterAcrossSelection ' A GARDER
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub
Sub Macro_APRES_EPUREMENT()
    Range("C2:G2").HorizontalAlignment = xlCenterAcrossSelection
End Sub

Si ça ne suffit pas, merci de joindre ton fichier.

Cordialement.

Rechercher des sujets similaires à "centrer fusionner"