Besoin d'aide VBA: Compter nombre de caractère et faire un ajustement automatique

Bérel Kaëlig

XLDnaute Nouveau
Bonjour,
Je suis sur un TP en stage et je n'arrive pas à faire un ajustement automatique avec commentaire.

Il faudrait enfaite compter le nombre maximum de caractère de la plus grand ligne de la cellule et après faire l'autofit.
Serez t-il possible de m'aider svp ?

Voici le code:

s = Split(Range("A3"), vbLf)
maxc = -1: maxi = -1
For i = LBound(s) To UBound(s)
If Len(s(i)) > maxc Then maxc = Len(s(i)): maxi = i
Next i

ActiveSheet.Range("B5:Y11").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.EntireColumn.AutoFit
End With

Voilà le code sauf que j'aimerai compter le nombre de caractère mais pas les afficher et faire l'autofit de la cellule B5 à Y11

Bonne Journée

Bérel Kaëlig
 

vgendron

XLDnaute Barbatruc
Hello

avec un fichier exemple, ca nous permettrait de mieux comprendre le besoin..
tu veux régler la taille de la colonne en fonction du nombre de caractères sur la plus grand ligne de la cellule?

ex:
VB:
Sub test()
s = Split(Range("A1"), vbLf)
maxc = -1
For i = LBound(s) To UBound(s)
    If Len(s(i)) > maxc Then maxc = Len(s(i))
Next i

With ActiveSheet.Range("A1")
    .EntireColumn.ColumnWidth = maxc
End With

End Sub
 

Bérel Kaëlig

XLDnaute Nouveau
Bonjour
merci de ta réponse ! :)
Oui voilà je veux que ma plage de cellule B5 à Y11 ce règle automatiquement avec la plus grande ligne de la cellule :)
Je ne peux pas mettre de fichier car ce serait diffusé de données importantes d'une entreprise :)

Bonne journée
Bérel Kaëlig
 

vgendron

XLDnaute Barbatruc
avec ca alors.. peut etre
VB:
Sub LargeurColonne()

For i = 2 To 25
    maxc = -1
    For j = 5 To 11
        s = Split(Cells(j, i), vbLf)
        For k = LBound(s) To UBound(s)
            maxc = WorksheetFunction.Max(maxc, Len(s(k)))
        Next k
    Next j
    If maxc = -1 Then maxc = 10
    With ActiveSheet.Columns(i)
        .EntireColumn.ColumnWidth = maxc
    End With
Next i
End Sub
 

Bérel Kaëlig

XLDnaute Nouveau
Merci :)

avec ca alors.. peut etre
VB:
[/QUOTE]


[code]Sub LargeurColonne()
For i = 2 To 25
    maxc = -1
    For j = 5 To 11
        s = Split(Cells(j, i), vbLf)
        For k = LBound(s) To UBound(s)
            maxc = WorksheetFunction.Max(maxc, Len(s(k)))
        Next k
    Next j
    If maxc = -1 Then maxc = 10
    With ActiveSheet.Columns(i)
        .EntireColumn.ColumnWidth = maxc
    End With
Next i
End Sub

Tu peux adapter dans ce code là stp ? :)

Code:
   s = Split(Range("A1"), vbLf)
maxc = -1
For i = LBound(s) To UBound(s)
     If Len(s(i)) > maxc Then maxc = Len(s(i))
Next i
 
    ActiveSheet.Range("B5:Y11").Select
    With Selection
        '.ColumnWidth = .ColumnWidth * 8
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        '.EntireColumn.AutoFit
    End With
 
Dernière édition:

Discussions similaires

Réponses
8
Affichages
620

Statistiques des forums

Discussions
311 730
Messages
2 081 991
Membres
101 856
dernier inscrit
Marina40