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

Macro de format pour tableau

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

isa44

XLDnaute Occasionnel
Bonjour ,
pour un tableau A11😛2500, je voudrais appliquer la macro suivante aux lignes (de la colonne A à la colonne P) si dans la ligne A.😛. se trouve une valeure .

Code:
Sub form()
     
    
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=CELLULE(""row"")=LIGNE()"
    With Selection.FormatConditions(1).Font
        .Bold = True
        .Italic = False
        .ColorIndex = 3
    End With
    With Selection.FormatConditions(1).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(1).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions(1).Interior.ColorIndex = 6
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=MOD(LIGNE();2)=0"
    With Selection.FormatConditions(2).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(2).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions(2).Interior.ColorIndex = 15
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=MOD(LIGNE();1)=0"
    With Selection.FormatConditions(3).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(3).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(3).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.FormatConditions(3).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.FormatConditions(3).Interior.Pattern = xlNone
   
   
End Sub
 
Re : Macro de format pour tableau

Bonjour isa44,

Pourquoi des bordures dans la MFC ? Puisque ce sont toujours les mêmes, autant les appliquer aux cellules dès le début.

La 3ème condition est inutile puisque la formule =MOD(LIGNE();1)=0 renvoie toujours VRAI...

En conséquence, une macro beaucoup plus simple :

Code:
Sub form()

With [COLOR="Red"]Range("A11:P2500")[/COLOR]
  
  .Borders(xlLeft).LineStyle = xlContinuous
  .Borders(xlRight).LineStyle = xlContinuous
  .Borders(xlTop).LineStyle = xlContinuous
  .Borders(xlBottom).LineStyle = xlContinuous
  .Interior.Pattern = xlNone

  .FormatConditions.Delete

  .FormatConditions.Add Type:=xlExpression, Formula1:="=CELLULE(""row"")=LIGNE()"
  With .FormatConditions(1)
    .Font.Bold = True
    .Font.Italic = False
    .Font.ColorIndex = 3
    .Interior.ColorIndex = 6
  End With

  .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(LIGNE();2)=0"
  .FormatConditions(2).Interior.ColorIndex = 15

End With
End Sub

Edit : vous avez aussi écrit :

si dans la ligne A.😛. se trouve une valeure

Je vous laisse vous débrouiller car je n'ai pas compris.

Peut-être modifier les formules de la MFC ?

A+
 
Dernière édition:
Re : Macro de format pour tableau

Merci Job75 en effet c'est plus simple.

Mais je voudrais affecter cette macro seulement sur les lignes qu contiennent des données. Autrement dit je ne veux pas appliquer le format aux lignes qui ont les cellules de A à P totalement vides.
 
Re : Macro de format pour tableau

Re,

Je pense avoir compris que c'est ça que vous voulez :

Code:
Sub form()
Dim lig As Integer, plage As Range

For lig = 11 To 2500
  With Range(Cells(lig, "A"), Cells(lig, "P"))
    'si la ligne n'est pas vide on l'unit à plage
    If Application.CountA(.Cells) Then _
      Set plage = Union(IIf(plage Is Nothing, .Cells, plage), .Cells)
  End With
Next

'efface la MFC et les bordures
With Range("A11:P2500")
  .FormatConditions.Delete
  .Borders(xlLeft).LineStyle = xlNone
  .Borders(xlRight).LineStyle = xlNone
  .Borders(xlTop).LineStyle = xlNone
  .Borders(xlBottom).LineStyle = xlNone
  .Interior.Pattern = xlNone
End With

If plage Is Nothing Then Exit Sub 'si tout est vide...
    
With plage
  
  'crée les bordures
  .Borders(xlLeft).LineStyle = xlContinuous
  .Borders(xlRight).LineStyle = xlContinuous
  .Borders(xlTop).LineStyle = xlContinuous
  .Borders(xlBottom).LineStyle = xlContinuous
  .Interior.Pattern = xlNone
  
  'MFC 1ère condition
  .FormatConditions.Add Type:=xlExpression, Formula1:="=CELLULE(""row"")=LIGNE()"
  With .FormatConditions(1)
    .Font.Bold = True
    .Font.Italic = False
    .Font.ColorIndex = 3
    .Interior.ColorIndex = 6
  End With
  
  'MFC 2ème condition
  .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(LIGNE();2)=0"
  .FormatConditions(2).Interior.ColorIndex = 15
  
End With

End Sub

A+
 
Re : Macro de format pour tableau

Re,

Finalement une solution bien meilleure, à partir de la 1ère macro.

Une condition supplémentaire (la 1ère) est créée pour effacer les bordures et la couleur de fond si la ligne est vide.

La formule en A11 => =NBVAL($A11:$P11)=0

Code:
Sub form()
With Range("A11:P2500")

  .Borders(xlLeft).LineStyle = xlContinuous
  .Borders(xlRight).LineStyle = xlContinuous
  .Borders(xlTop).LineStyle = xlContinuous
  .Borders(xlBottom).LineStyle = xlContinuous
  .Interior.Pattern = xlNone

  .FormatConditions.Delete
  
  .FormatConditions.Add Type:=xlExpression, _
    Formula1:="[COLOR="Red"]=NBVAL($A" & ActiveCell.Row & ":$P" & ActiveCell.Row & ")=0[/COLOR]"
  With .FormatConditions(1)
    .Borders(xlLeft).LineStyle = xlNone
    .Borders(xlRight).LineStyle = xlNone
    .Borders(xlTop).LineStyle = xlNone
    .Borders(xlBottom).LineStyle = xlNone
    .Interior.Pattern = xlNone
  End With


  .FormatConditions.Add Type:=xlExpression, Formula1:="=CELLULE(""row"")=LIGNE()"
  With .FormatConditions(2)
    .Font.Bold = True
    .Font.Italic = False
    .Font.ColorIndex = 3
    .Interior.ColorIndex = 6
  End With

  .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(LIGNE();2)=0"
  .FormatConditions(3).Interior.ColorIndex = 15

End With
End Sub

L'intérêt de cette solution est que la 1ère condition agit immédiatement si l'on efface des lignes, pas besoin de relancer la macro.

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
14
Affichages
250
Réponses
9
Affichages
590
  • Question Question
Microsoft 365 comparaison texte
Réponses
5
Affichages
715
Réponses
2
Affichages
785
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…