comment faire une boucle

Bricoltou

XLDnaute Occasionnel
Bonjour le forum

je débute en VBA et je souhaiterais faire une boucle avec le code ci dessous :

Pouvez vous m'expliquer comment faire :confused:

Merci d'avance pour vos réponses ;)

bricoltou

Code:
          Range("B16:C16").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B17:C17").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B18:C18").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B19:C19").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B20:C20").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B21:C21").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B22:C22").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
     Range("B23:C23").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
 

Bricoltou

XLDnaute Occasionnel
Re : comment faire une boucle

Bonjour le forum
je débute en VBA et je souhaiterais faire une boucle avec le code ci dessous :
Pouvez vous m'expliquer comment faire :confused:
Merci d'avance pour vos réponses ;)
bricoltou
mon code
Range("B16:C16").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E16:F16").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("H16:I16").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("B17:C17").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("B18:C18").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("B19:C19").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E18:F18").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E19:F19").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E20:F20").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E21:F21").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E22:F22").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
Range("E23:F23").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
 

Dranreb

XLDnaute Barbatruc
Re : comment faire une boucle

Bonjour.
Plutôt, pour éviter le Merge, ce que je conseille :
VB:
.HorizontalAlignment = xlCenterAcrossSelection
Ce qui ne répond toujours pas à la question !
Mais ça illustre qu'il faut toujours étudier comment on pourrait demander à Excel de l'effectuer, cette fameuse boucle.
Sinon, quand c'est vraiment impossible, derrière le With de mécano41, c'est du genre For Each Plg In .Rows et travailler avec Plg.Columns(C) ou bien For L = 1 To .Rows.Count et travailler avec .Rows(L) ou .Cells(L, C).
Dim Plg As Range ou bien L As Long, C as Long ou spécifier des constantes à la place.

Edit: Oh, mécano41, pourquoi as tu supprimé ton message ?
L'idée d'une action sur l'ensemble de la plage était excellente !
Même si tu avais oublié qu'il voulait que le contenu des cellules apparaisse centré sur plusieurs colonnes, ce qui peut se faire sans Merge !
 
Dernière édition:

mécano41

XLDnaute Accro
Re : comment faire une boucle

... derrière le With de mécano41....

J'ai supprimé le message car j'avais écrit une ânerie...(pas vu le Merge...)

Voici un code possible (appliquant le conseil de Dranreb car évite certains problèmes de cellules fusionnées dans VBA)

Code:
Option Explicit

Sub Essai()
Dim Plage As Range
Dim NbLignes As Long
Dim Lig As Long

Set Plage = Range("B16:C23")
NbLignes = Plage.Rows.Count
For Lig = 16 To 16 + NbLignes
    With Range("B" & Lig).Resize(1, 2)
        .HorizontalAlignment = xlCenterAcrossSelection
        .VerticalAlignment = xlBottom
    End With
Next Lig
End Sub

Cordialement
 

mécano41

XLDnaute Accro
Re : comment faire une boucle

Voilà...

Code:
Option Explicit

Sub Essai()
' Déclaration des variables
Dim Plage As Range
Dim NbLignes As Long
Dim Lig As Long

Set Plage = Range("B16:C23")                                       ' Définit l'emplacement des données dans une variable "Range"
NbLignes = Plage.Rows.Count                                       ' Compte le Nb. de ligne de la plage de données
For Lig = 16 To 16 + NbLignes                                       ' Balaye la plage depuis la ligne 16 jusqu'à la dernière
    With Range("B" & Lig).Resize(1, 2)                            ' "B" & Lig désigne chaque cellule de la colonne B de la plage
                                                                                   '  et Resize(1,2) dit de prendre 1 ligne et deux colonnes de cellules (donc 2 cellules)
        .HorizontalAlignment = xlCenterAcrossSelection      ' Applique le cenrage sur l'ensemble des 2 cellules (sans les fusionner)
                                                                                   ' idem à centrage horizontal sur plusieurs colonnes en manuel
        .VerticalAlignment = xlBottom                                ' Applique le centrage vertical
    End With
Next Lig
End Sub

Cordialement
 

Si...

XLDnaute Barbatruc
Re : comment faire une boucle

salut

pour des alignements centrés sur 2 colonnes -Resize(1,2)-avec des cellules dispersées (pas en trop grand nombre) peut-être ainSi...
Code:
Sub Formate()
  Dim n As Byte
  For n = 0 To 12
    '_ pour un retour à ligne seulement en écriture
    With Range(Array("B16", "B17", "B18", "B19", _
                "E16", "E17", "E18", "E19", "E20", _
                "E21", "E22", "E23", "H16")(n)).Resize(1, 2)
      .HorizontalAlignment = xlCenterAcrossSelection
      .VerticalAlignment = xlBottom
    End With
  Next
End Sub
 

Statistiques des forums

Discussions
312 531
Messages
2 089 372
Membres
104 149
dernier inscrit
Kaizho