Format affichage personnalisé

domburg

XLDnaute Nouveau
Bonjour,

Je cherche à afficher dans Excel une chaîne de 10 caractères avec des séparateurs tous les 2 caractères.
Exemple 1 : pour une valeur 1234567890 => afficher 12 34 56 78 90
Exemple 2 : Pour une valeur AB34567890 => afficher AB 34 56 78 90
Si j'utilise le format personnalisé 00" "00" "00" "00" "00, ça fonctionne pour les valeurs numériques comme dans l'exemple 1
S'il y a au moins un caractère texte (comme dans l'exemple 2), ça ne fonctionne pas.
Quelqu'un a t'il une solution

Merci d'avance
 

excfl

XLDnaute Barbatruc
Re : Format affichage personnalisé

Bonsoir le forum,

=GAUCHE(A1;2)&" "&DROITE(GAUCHE(A1;3))&DROITE(GAUCHE(A1;4))&" "&DROITE(GAUCHE(A1;5))&DROITE(GAUCHE(A1;6))&" "&DROITE(GAUCHE(A1;7))&DROITE(GAUCHE(A1;8))&" "&DROITE(GAUCHE(A1;9))&DROITE(GAUCHE(A1;10))

excfl
 

Pièces jointes

  • format.xlsx
    9.7 KB · Affichages: 55
  • format.xlsx
    9.7 KB · Affichages: 55
  • format.xlsx
    9.7 KB · Affichages: 64
Dernière édition:

mapomme

XLDnaute Barbatruc
Supporter XLD
Re : Format affichage personnalisé

Bonsoir domburg,

Un essai par macro. Le code est dans le module de Feuil1:
VB:
Sub formater()
Dim xrg As Range, xcell As Range, s As String
Dim i As Long, i0 As Long, L As Long, Tablo

Application.ScreenUpdating = False
Set xrg = Range("A" & Rows.Count).End(xlUp)
Set xrg = Range(Range("A1"), xrg)

For Each xcell In xrg
  If Len(xcell) <> 0 Then
    If Len(xcell) Mod 2 = 1 Then s = " " & xcell Else s = xcell
    L = Len(s) \ 2
    i0 = Len(s)
    ReDim Tablo(1 To L)
    For i = i0 To 2 Step -2
      Tablo(i \ 2) = Mid(s, i - 1, 2)
    Next i
    xcell = LTrim(Join(Tablo))
  End If
Next xcell
Application.ScreenUpdating = True
End Sub

Edit: préférer la version v2 ici #5
 
Dernière édition:

mapomme

XLDnaute Barbatruc
Supporter XLD
Re : Format affichage personnalisé

Bonjour domburg,

Une version v2 qui, contrairement à la v1, autorise les formatages successifs.

Le code légèrement modifié:
VB:
Sub formater()
Dim xrg As Range, xcell As Range, s As String
Dim i As Long, i0 As Long, L As Long, Tablo

Application.ScreenUpdating = False
Set xrg = Range("A" & Rows.Count).End(xlUp)
Set xrg = Range(Range("A1"), xrg)

For Each xcell In xrg
  If Len(xcell) <> 0 Then
    Tablo = Split(xcell)
    s = Join(Tablo, "")
    If Len(s) Mod 2 = 1 Then s = " " & s
    L = Len(s) \ 2
    i0 = Len(s)
    ReDim Tablo(1 To L)
    For i = i0 To 2 Step -2
      Tablo(i \ 2) = Mid(s, i - 1, 2)
    Next i
    xcell = LTrim(Join(Tablo))
  End If
Next xcell
Application.ScreenUpdating = True
End Sub
 

Pièces jointes

  • format v2.xls
    43.5 KB · Affichages: 72
  • format v2.xls
    43.5 KB · Affichages: 72
  • format v2.xls
    43.5 KB · Affichages: 59
Dernière édition:

Discussions similaires

Réponses
7
Affichages
351

Statistiques des forums

Discussions
312 223
Messages
2 086 403
Membres
103 201
dernier inscrit
centrale vet