Aide - Simplifier le code

  • Initiateur de la discussion Initiateur de la discussion Lone-wolf
  • 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 !

Lone-wolf

XLDnaute Barbatruc
Bonjour à tous,

j'aimerais simplifier le code que voici:

Code:
   Range("E2").Value = "A": Range("E2").Font.Bold = True: Range("E2").Font.Color = RGB(255, 255, 255)
   Range("F2").Value = "B": Range("F2").Font.Bold = True: Range("F2").Font.Color = RGB(255, 255, 255)
   Range("G2").Value = "C": Range("G2").Font.Bold = True: Range("G2").Font.Color = RGB(255, 255, 255)
   Range("H2").Value = "D": Range("H2").Font.Bold = True: Range("H2").Font.Color = RGB(255, 255, 255)
   Range("I2").Value = "E": Range("I2").Font.Bold = True: Range("I2").Font.Color = RGB(255, 255, 255)
   Range("J2").Value = "F": Range("J2").Font.Bold = True: Range("J2").Font.Color = RGB(255, 255, 255)
   Range("K2").Value = "G": Range("K2").Font.Bold = True: Range("K2").Font.Color = RGB(255, 255, 255)
   Range("L2").Value = "H": Range("L2").Font.Bold = True: Range("L2").Font.Color = RGB(255, 255, 255)
   Range("M2").Value = "I": Range("M2").Font.Bold = True: Range("M2").Font.Color = RGB(255, 255, 255)
   Range("N2").Value = "J": Range("N2").Font.Bold = True: Range("N2").Font.Color = RGB(255, 255, 255)
   
   Range("D3").Value = "1": Range("D3").Font.Bold = True: Range("D3").Font.Color = RGB(255, 255, 255)
   Range("D4").Value = "2": Range("D4").Font.Bold = True: Range("D4").Font.Color = RGB(255, 255, 255)
   Range("D5").Value = "3": Range("D5").Font.Bold = True: Range("D5").Font.Color = RGB(255, 255, 255)
   Range("D6").Value = "4": Range("D6").Font.Bold = True: Range("D6").Font.Color = RGB(255, 255, 255)
   Range("D7").Value = "5": Range("D7").Font.Bold = True: Range("D7").Font.Color = RGB(255, 255, 255)
   Range("D8").Value = "6": Range("D8").Font.Bold = True: Range("D8").Font.Color = RGB(255, 255, 255)
   Range("D9").Value = "7": Range("D9").Font.Bold = True: Range("D9").Font.Color = RGB(255, 255, 255)
   Range("D10").Value = "8": Range("D10").Font.Bold = True: Range("D10").Font.Color = RGB(255, 255, 255)
   Range("D11").Value = "9": Range("D11").Font.Bold = True: Range("D11").Font.Color = RGB(255, 255, 255)
   Range("D12").Value = "10": Range("D12").Font.Bold = True: Range("D12").Font.Color = RGB(255, 255, 255)

Merci pour votre aide


A+ 😎
 

Pièces jointes

Re : Aide - Simplifier le code

Bonjour,

tu peux essayer ceci :
Code:
   For t = 1 To 10
     With Range("D2").Offset(0, t)
       .Value = Chr(64 + t)
       .Font.Bold = True
       .Font.Color = RGB(255, 255, 255)
     End With
     With Range("D2").Offset(t, 0)
       .Value = Chr(48 + t)
       .Font.Bold = True
       .Font.Color = RGB(255, 255, 255)
     End With
     
Next t
[D12] = 10
 
Re : Aide - Simplifier le code

Salut Lone-wolf et le forum
Une approche différente, pour le même résultat : construction d'un damier
Code:
Sub test()
Dim X As Integer
For X = 1 To 10
    Range("D2").Offset(X, 0) = X
    Range("D2").Offset(0, X) = Chr(64 + X)
Next X
With Range("D2:O13")
    .Font.Bold = True
    .Font.ColorIndex = 2
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .Interior.Color = RGB(128, 71, 41)
End With
With Range("E3:N12")
    .Font.ColorIndex = 0
    For X = 2 To Range("E3:N12").Cells.Count Step 2
        .Cells(X - (.Cells(X).Row Mod 2)).Interior.ColorIndex = 1
    Next X
End With
End Sub
A+
 
- 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

Discussions similaires

Réponses
7
Affichages
619
Réponses
1
Affichages
1 K
Réponses
13
Affichages
2 K
Réponses
2
Affichages
868
Réponses
4
Affichages
2 K
Réponses
17
Affichages
2 K
Réponses
12
Affichages
2 K
Retour