Format d’une colonne différents selon la nature de la cellule

S

Stéphane

Guest
Bonsoir à tous,

J’ai une question peut-être un peu stupide pour les Pros, mais qui est pour moi un véritable casse tête.

Est-il possible dans une même colonne d’aligner à droite les cellules en format texte et de centrer celles qui sont en format numérique ?

J’imagine qu’il faut passer par du VBA ?

Merci pour votre réponse à bientôt.

Stéphane.
 
V

Vériland

Guest
Bonsoir Stéphane et toi le Forum,

Voilà la macro qui te permettra d'effectuer cette requête...

toutes les données numériques présentes sur l'ensemble de la feuille seront centrées et les valeurs texte seront alignées à droite...

Option Explicit

Sub Alignement()
' Vériland
' http://www.excel-downloads.com/html/French/forum/messages/1_49898_49898.htm
Dim MaPlage As Range
Dim C As Variant
Application.ScreenUpdating = False
Set MaPlage = ActiveSheet.UsedRange
On Error Resume Next
For Each C In MaPlage
If IsNumeric(C.Value) = False Then
With C
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else
With C
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
End With
End If
Next
Set MaPlage = Nothing
Application.ScreenUpdating = True
End Sub


Bonne programmation

A+Veriland.gif


PS : Macro à copier depuis le forum auquel cas tu risques d'avoir les attributs gras du post dedans...
 
V

Vériland

Guest
Oups...autant pour moi...j'ai pas fait attention que tu parlais d'une colonne...donc voici la correction de la macro...

Option Explicit

Sub Alignement()
' Vériland
' http://www.excel-downloads.com/html/French/forum/messages/1_49898_49898.htm
Dim MaPlage As Range
Dim C As Variant
Application.ScreenUpdating = False
Set MaPlage = Range("A1:A500")
On Error Resume Next
For Each C In MaPlage
If IsNumeric(C.Value) = True Then
With C
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else
With C
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
End With
End If
Next
Set MaPlage = Nothing
Application.ScreenUpdating = True
End Sub


Cette macro agit sur la plage A1:A500...à modifier selon tes besoins...

A+Veriland.gif


PS : Macro à copier depuis le forum auquel cas tu risques d'avoir les attributs gras du post dedans...
 
S

Stéphane

Guest
Bonsoir le Forum,

Merci beaucoup Veriland, cela fct bien.

Par contre j'ai essayé de la faire fctionner sur plusieurs feuille en même tps avec Sheets(array...) mais cela ne marche pas !

Aurais-tu une solution ?

Encore merci.

Stéphane
 
V

Vériland

Guest
Bonjour Stéphane et toi le Forum,

Oui j'ai cette solution...

Option Explicit

Sub Alignement()
' Vériland
' http://www.excel-downloads.com/html/French/forum/messages/1_49898_49898.htm
Dim MaPlage As Range
Dim C As Variant
Dim Feuille As Variant
Application.ScreenUpdating = False
On Error Resume Next
For Each Feuille In Sheets
With Feuille
.Activate
Set MaPlage = Range("A1:A500")
For Each C In MaPlage
If IsNumeric(C.Value) = True Then
With C
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else
With C
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
End With
End If
Next
End With
Next Feuille
Set MaPlage = Nothing
Application.ScreenUpdating = True
End Sub


Le principe est simple...on fait une boucle sur l'ensemble des feuilles du classeur et sur chacune d'elle on applique l'alignement des nombres ou du texte sur la plage A1:A500...

A+Veriland.gif


PS : Macro à copier depuis le forum auquel cas tu risques d'avoir les attributs gras du post dedans...
 

Discussions similaires

  • Question
Microsoft 365 Code VBA
Réponses
3
Affichages
421

Statistiques des forums

Discussions
314 123
Messages
2 106 144
Membres
109 503
dernier inscrit
clercst