Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

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



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...



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...



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
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…