Macro de format pour tableau

isa44

XLDnaute Occasionnel
Bonjour ,
pour un tableau A11:p2500, je voudrais appliquer la macro suivante aux lignes (de la colonne A à la colonne P) si dans la ligne A.:p. 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
 

job75

XLDnaute Barbatruc
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.:p. 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:

isa44

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

job75

XLDnaute Barbatruc
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+
 

job75

XLDnaute Barbatruc
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+
 

Statistiques des forums

Discussions
312 755
Messages
2 091 707
Membres
105 053
dernier inscrit
HAMOUD