• Initiateur de la discussion Initiateur de la discussion chouill
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

C

chouill

Guest
bonjour,

je travail actuellement sur un tableau de valeurs, celles ci son rentrées dans des cellules dont le format est spécifié par une macro que voici :

Sub test()

Range("A1:E1").Select
ActiveCell.FormulaR1C1 = "1 Pré conditionement :"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With


Range("O1😛1").Select
ActiveCell.FormulaR1C1 = "Réaliser le :"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With


Range("Q1:R1").Select
ActiveCell.FormulaR1C1 = "6/21/2009"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With


...

comme on peut le constater la ligne de code en rouge est redondante j'aimerais la remplacer par une variable du type "

Dim FormatCal as ....

FormatCel = Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With

merci de votre aide
 
Re : varible formater

Bonsoir

essaye ainsi :

Code:
Range("A1:E1").FormulaR1C1 = "1 Pré conditionement :"
Range("O1:P1").FormulaR1C1 = "Réaliser le :"
Range("Q1:R1").FormulaR1C1 = "6/21/2009"

With Range("A1:E1,O1:R1,")
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .ShrinkToFit = False
    .MergeCells = True
End With

bonne soirée
@+
 
Re : varible formater

merci pour cette réponse rapide mais ma mise en forme va intervenir beaucoup, beaucoup, beaucoup de fois, j'aurais vraiment voulu en faire une variable au début de mon classeur.

une demo de mon code :

Range("A1:E1").Select
ActiveCell.FormulaR1C1 = "1 Pré conditionement :"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With

Range("O1😛1").Select
ActiveCell.FormulaR1C1 = "Réaliser le :"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With

Range("Q1:R1").Select
ActiveCell.FormulaR1C1 = "6/21/2009"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With

...

Range("A3:B5").Select
ActiveCell.FormulaR1C1 = "réf. Cellulose"
With ActiveCell.Characters(Start:=1, Length:=0).Font
.Name = "Times New Roman"
.FontStyle = "Gras"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Range("A6:B6").Select
ActiveCell.FormulaR1C1 = "RJF00188"
With ActiveCell.Characters(Start:=1, Length:=0).Font
.Name = "Times New Roman"
.FontStyle = "Normal"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Range("C3:F3").Select
ActiveCell.FormulaR1C1 = "masse initial"
Range("C4😀4").Select
ActiveCell.FormulaR1C1 = "avant étuve"
With ActiveCell.Characters(Start:=1, Length:=0).Font
.Name = "Times New Roman"
.FontStyle = "Normal"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("E4:F4").Select
ActiveCell.FormulaR1C1 = "apré etuve"
With ActiveCell.Characters(Start:=1, Length:=0).Font
.Name = "Times New Roman"
.FontStyle = "Normal"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("G3:J3").Select
ActiveCell.FormulaR1C1 = "Humidité"
Range("G4:H4").Select
ActiveCell.FormulaR1C1 = "etuve"
Range("I4:J4").Select
ActiveCell.FormulaR1C1 = "salle"
Range("K3:N3").Select
ActiveCell.FormulaR1C1 = "temperature"
Range("K4:L4").Select
ActiveCell.FormulaR1C1 = "etuve"
Range("M4:N4").Select
ActiveCell.FormulaR1C1 = "salle"
Range("O3:R3").Select
ActiveCell.FormulaR1C1 = "dimention"
Range("O4😛4").Select
ActiveCell.FormulaR1C1 = "longeur"
Range("Q4:R4").Select
ActiveCell.FormulaR1C1 = "largeur"
Range("C5😀5").Select
ActiveCell.FormulaR1C1 = "g"
Range("E5:F5").Select
ActiveCell.FormulaR1C1 = "g"
Range("G5:H5").Select
ActiveCell.FormulaR1C1 = "%"
Range("I5:J5").Select
ActiveCell.FormulaR1C1 = "%"
Range("K5:L5").Select
ActiveCell.FormulaR1C1 = "°C"
Range("M5:N5").Select
ActiveCell.FormulaR1C1 = "°C"
Range("O5😛5").Select
ActiveCell.FormulaR1C1 = "cm"
Range("Q5:R5").Select
ActiveCell.FormulaR1C1 = "cm"
Range("C6😀6").Select
ActiveCell.FormulaR1C1 = "217.6"
Range("E6:F6").Select
ActiveCell.FormulaR1C1 = "200"
Range("G6:H6").Select
ActiveCell.FormulaR1C1 = "35"
Range("I6:J6").Select
ActiveCell.FormulaR1C1 = "45"
Range("K6:L6").Select
ActiveCell.FormulaR1C1 = "40"
Range("M6:N6").Select
ActiveCell.FormulaR1C1 = "20"
Range("O6😛6").Select
ActiveCell.FormulaR1C1 = "241"
Range("Q6:R6").Select
ActiveCell.FormulaR1C1 = "42"
Range("B9😀9").Select
ActiveCell.FormulaR1C1 = "Observations :"
Range("A10:R10").Select
ActiveCell.FormulaR1C1 = "<> redecoupe de la cellulose"
With ActiveCell.Characters(Start:=1, Length:=0).Font
.Name = "Times New Roman"
.FontStyle = "Normal"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
End Sub
 
Re : varible formater

Bonjour à tous,
Salut Pierrot 🙂🙂 (ah ben ça fait un siècle)

Merci à chouill d'utiliser les balises code pour déposer du... code (le # dans la barre d'outils) :

Code:
Range("A1:E1").Select
    ActiveCell.FormulaR1C1 = "1 Pré conditionement :"
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    
    Range("O1:P1").Select
    ActiveCell.FormulaR1C1 = "Réaliser le :"
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    
    Range("Q1:R1").Select
    ActiveCell.FormulaR1C1 = "6/21/2009"
        With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    
    ...
    
    Range("A3:B5").Select
    ActiveCell.FormulaR1C1 = "réf. Cellulose"
    With ActiveCell.Characters(Start:=1, Length:=0).Font
        .Name = "Times New Roman"
        .FontStyle = "Gras"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    Range("A6:B6").Select
    ActiveCell.FormulaR1C1 = "RJF00188"
    With ActiveCell.Characters(Start:=1, Length:=0).Font
        .Name = "Times New Roman"
        .FontStyle = "Normal"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    Range("C3:F3").Select
    ActiveCell.FormulaR1C1 = "masse initial"
    Range("C4:D4").Select
    ActiveCell.FormulaR1C1 = "avant étuve"
    With ActiveCell.Characters(Start:=1, Length:=0).Font
        .Name = "Times New Roman"
        .FontStyle = "Normal"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("E4:F4").Select
    ActiveCell.FormulaR1C1 = "apré etuve"
    With ActiveCell.Characters(Start:=1, Length:=0).Font
        .Name = "Times New Roman"
        .FontStyle = "Normal"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("G3:J3").Select
    ActiveCell.FormulaR1C1 = "Humidité"
    Range("G4:H4").Select
    ActiveCell.FormulaR1C1 = "etuve"
    Range("I4:J4").Select
    ActiveCell.FormulaR1C1 = "salle"
    Range("K3:N3").Select
    ActiveCell.FormulaR1C1 = "temperature"
    Range("K4:L4").Select
    ActiveCell.FormulaR1C1 = "etuve"
    Range("M4:N4").Select
    ActiveCell.FormulaR1C1 = "salle"
    Range("O3:R3").Select
    ActiveCell.FormulaR1C1 = "dimention"
    Range("O4:P4").Select
    ActiveCell.FormulaR1C1 = "longeur"
    Range("Q4:R4").Select
    ActiveCell.FormulaR1C1 = "largeur"
    Range("C5:D5").Select
    ActiveCell.FormulaR1C1 = "g"
    Range("E5:F5").Select
    ActiveCell.FormulaR1C1 = "g"
    Range("G5:H5").Select
    ActiveCell.FormulaR1C1 = "%"
    Range("I5:J5").Select
    ActiveCell.FormulaR1C1 = "%"
    Range("K5:L5").Select
    ActiveCell.FormulaR1C1 = "°C"
    Range("M5:N5").Select
    ActiveCell.FormulaR1C1 = "°C"
    Range("O5:P5").Select
    ActiveCell.FormulaR1C1 = "cm"
    Range("Q5:R5").Select
    ActiveCell.FormulaR1C1 = "cm"
    Range("C6:D6").Select
    ActiveCell.FormulaR1C1 = "217.6"
    Range("E6:F6").Select
    ActiveCell.FormulaR1C1 = "200"
    Range("G6:H6").Select
    ActiveCell.FormulaR1C1 = "35"
    Range("I6:J6").Select
    ActiveCell.FormulaR1C1 = "45"
    Range("K6:L6").Select
    ActiveCell.FormulaR1C1 = "40"
    Range("M6:N6").Select
    ActiveCell.FormulaR1C1 = "20"
    Range("O6:P6").Select
    ActiveCell.FormulaR1C1 = "241"
    Range("Q6:R6").Select
    ActiveCell.FormulaR1C1 = "42"
    Range("B9:D9").Select
    ActiveCell.FormulaR1C1 = "Observations :"
    Range("A10:R10").Select
    ActiveCell.FormulaR1C1 = "<> redecoupe de la cellulose"
    With ActiveCell.Characters(Start:=1, Length:=0).Font
        .Name = "Times New Roman"
        .FontStyle = "Normal"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
End Sub
Pour info :
Code:
Range("M6:N6").Select
     ActiveCell.FormulaR1C1 = "20"
est égale à :
Code:
Range("M6:N6").Formula = "20"
ou
Code:
Range("M6:N6")= "20"
le value étant implicite

Un "adage" "vidéo" de l'ami Didier :



A++ 🙂🙂
A+ à tous
 
Re : varible formater

Bonsoir Chouill
Salut JCGL,Pierrot et bonsoir le forum

Chouill, si tes mises en forme se répétent, tu peux créer une sub qui s'en charge
Par exemple tu écris ceci:
Code:
Sub mef(Zone As Range)
With Zone
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
End With
End Sub
Et ensuite tu t'en sers dans ta macro principale
Code:
Sub test()
Call mef(Range("A1:E1"))
Call mef(Range("O1:P1"))
End Sub

En espérant que cela pourra t'aider,

Bonne soirée à tous,

Ninbihan
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
1 K
Réponses
7
Affichages
377
Réponses
17
Affichages
1 K
Retour