Lancer la macro pour les cellules non vides

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

ascal44

XLDnaute Occasionnel
Bonsoir à tous , je voudrais lancer la macro " couleur" pour les lignes 45 à 150 si sur ces lignes la cellule en colonne E contient une valeure.
Comment rédiger ce code VBA ?
 
Re : Lancer la macro pour les cellules non vides

Salut ascal44 et le forum
je voudrais lancer la macro " couleur" pour les lignes 45 à 150 si sur ces lignes la cellule en colonne E contient une valeur.
Une proposition :
Code:
Dim X as long
......
For X=45 to 150
    if cells(x, "E")<>"" then Call couleur
next x
Mais je suis prêt à parier que ça ne suffira pas : il faut transmettre le paramètre ligne ? Si oui, il suffit de mettre X en paramètre à transmettre, ou de prévoir X en variable globale, ou d'inclure ton traitement couleur dans le test.
A+
 
Re : Lancer la macro pour les cellules non vides

Je ne comprends pas , le format s'applique aussi si en E c'est vide

Sub form()

Dim X As Long

For X = 45 To 150
If Cells(X, "E") <> "" Then Call couleur
Next X

End Sub

Sub couleur()



'Supprime les formats conditionnels
Range("A45😛150").Select
With Selection
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.FormatConditions.Delete
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Range("E45").Select
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:= _
"=ET($E45<>"""";MOD(LIGNE();2)=0)"
'Selection.FormatConditions(2).Font.ColorIndex = xlAutomatic
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:="=$E45<>"""""
'Selection.FormatConditions(3).Font.ColorIndex = xlAutomatic
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



Rows("45:150").Select ' Hauteur de ligne
Selection.RowHeight = 25
End Sub
 
Re : Lancer la macro pour les cellules non vides

Bonsoir ascal44, Pierrot93 et Gorfael
La réponse est dans la remarque de Gorfael : il faut transmettre le paramètre ligne et ne sélectionner que la ligne dans sub couleur.
Je n'ai pas excel à la maison, sauf erreur tu devrais essayer les modifications suivantes
Code:
Sub form()

Dim X As Long

For X = 45 To 150
If Cells(X, "E") <> "" Then Call couleur(X)
Next X

End Sub
Code:
Sub couleur(Lgn as long)
'Supprime les formats conditionnels
Range("A" & Lgn & ":P" & Lgn).Select
With Selection
etc…
 
Re : Lancer la macro pour les cellules non vides

Salut ascal44 et le forum
Je ne comprends pas , le format s'applique aussi si en E c'est vide
Je sens qu'on va avoir du mal 😉. Telle que je lis ta macro : Tu supprimes la mise en forme des cellules A45😛150, ainsi que les formats conditionnels, une RAZ normale, quoi.
Puis ensuite tu recrées un format conditionnel sur la cellule E45, pas sur ta plage, juste sur E45 ! Donc, normalement si j'ai un peu compris le VBA, ton format ne peut s'appliquer si E46:E150 ont des cellules vides ou non, puisque tu n'y fait référence que pour la RAZ.

Mets un point d'arrêt sur sub form() et regarde ce qui se passe en pas-à-pas avec F8 avec tes macros originelles.
Ta macro, avec une petite mise en forme :
Code:
Sub couleur()
'Supprime les formats conditionnels
With Range("A45:P150")
    .Orientation = 0
    .AddIndent = False
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    .FormatConditions.Delete
End With
'""""""""""""""""""""""""""""""""""""""""""""""""" """"""""""""""""""""""""""""""""""""""""
With Range("E45")
    .FormatConditions.Delete 'encore ????
    .FormatConditions.Add Type:=xlExpression, Formula1:="=CELLULE(""row"")=LIGNE()"
    With .FormatConditions(1).Font
        .Bold = True
        .Italic = False
        '.ColorIndex = 3
    End With
    With .FormatConditions(1).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(1).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    .FormatConditions(1).Interior.ColorIndex = 6
    .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($E45<>"""";MOD(LIGNE();2)=0)"
    'Selection.FormatConditions(2).Font.ColorIndex = xlAutomatic
    With .FormatConditions(2).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(2).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(2).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(2).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    .FormatConditions(2).Interior.ColorIndex = 15
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$E45<>"""""
    '.FormatConditions(3).Font.ColorIndex = xlAutomatic
    With .FormatConditions(3).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(3).Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(3).Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With .FormatConditions(3).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End With
Rows("45:150").RowHeight = 25
End Sub
Avec les indentation, on sait où on se situe dans les with.
Quand même un peut de mal à savoir ce que tu veux faire réellement :
La hauteur de tes lignes concerne toutes les lignes de 45 à 150, sans discernement de la valeur de E
=CELLULE("row")=LIGNE()
Colorie en jaune la cellule de E sur laquelle tu es... si elle a les MFC, et s'il y a recalcul de la feuille. si on veut que ce soit constant, il faut une calculate à chaque changement de sélection.
La seconde condition :
Code:
=ET($E45<>"";MOD(LIGNE();2)=0)
met en gris les cellules des lignes paires dont E n'est pas vide
La troisième condition, met seulement un encadrement si E n'est pas vide

Donc, je suppose que tu veux appliquer ta MFC sur la cellule E45, puis l'étendre de E45 à P45, puis l'étendre de E45 à P150
ce qui doit donner :
Code:
Range("E45").copy
Range("A45:P150").PasteSpecial Paste:=xlPasteFormats
Pour la hauteur des lignes, c'est plus problématique : soit tu fait la boucle donnée, soit, si tes cellules de E sont bien vides, tu peux faire une instruction de ce type :
Code:
Rows("45:150").RowHeight = 25
Range("E45:E150").SpecialCells(xlCellTypeBlanks).RowHeight = 10
À ma connaissance, il n'y a pas de regroupement de ce type pour les cellules dont la valeur est ""
A+
 
Dernière édition:
Re : Lancer la macro pour les cellules non vides

Bonsoir Gorfael ,

En fait si je veux aussi discerner cette formule :
=CELLULE("row")=LIGNE()
si il n'y a rien en E.

Si je met
Range("E45").copy
Range("A45😛150").PasteSpecial Paste:=xlPasteFormats
cela me copie les formats conditionnels même si je n'ai rien en E

Je ne voudrais pas copier les formats conditionnels sur les lignes qui n'ont rien en E
 
Re : Lancer la macro pour les cellules non vides

Salut ascal44 et le forum
Code:
=(CELLULE("row")=LIGNE())*(E45<>"")
La formule
Code:
                              Range("E45").copy
Range("A45:P150").PasteSpecial Paste:=xlPasteFormats
Copie sans tenir compte de E. On est donc obligé de passer par une boucle :
Code:
Range("E45").copy
for x=45 to 150
    if range("E" & x)<>"" then Range(cells(x, "A"), cells(x, "P").PasteSpecial Paste:=xlPasteFormats 
next x
A+
 
Dernière édition:
Re : Lancer la macro pour les cellules non vides

Bonjour , encore des bugs avec ce code :
Range("E45").Copy
For X = 45 To 150
If Range("E" & X) <> "" Then
Range(Cells(X, "A"), Cells(X, "P")).PasteSpecial Paste:=xlPasteFormats
End If
Next X

j'ai l'erreur : la méthode PasteSpecial de la classe Range à échoué.

Cpmment remédier à cela ?
 
Re : Lancer la macro pour les cellules non vides

Salut ascal44 et le forum
j'ai copié le code, juste ajouté un titre et une déclaration :
Code:
Sub test()
dim x as long
Range("E45").Copy
For X = 45 To 150
    If Range("E" & X) <> "" Then
    Range(Cells(X, "A"), Cells(X, "P")).PasteSpecial Paste:=xlPasteFormats
    End If
Next X 			 		
End sub
Et il fonctionne correctement.
A+
 
Re : Lancer la macro pour les cellules non vides

Bonsoir , j'ai trouvé un code s'apparentant à ce que je voudrais.Mais il s'applique pour toutes les colonnes d'une plage. Comment la limiter pour la colonne E ?
Sub form()
Dim lig As Integer, plage As Range

Application.ScreenUpdating = False

Application.Calculation = xlManual ' accélère l'exécution
Application.DisplayAlerts = False




For lig = 45 To 150
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
Range(Cells(lig, "M"), Cells(lig, "P")).Select
Selection.Merge
Selection.HorizontalAlignment = xlLeft
Selection.VerticalAlignment = xlCenter
Next

'efface la MFC et les bordures
With Range("A1😛100")
.FormatConditions.Delete
.Borders(xlLeft).LineStyle = xlNone
.Borders(xlRight).LineStyle = xlNone
.Borders(xlTop).LineStyle = xlNone
.Borders(xlBottom).LineStyle = xlNone
'.Interior.ColorIndex = 43
End With

If plage Is Nothing Then Exit Sub 'si il n'y à rien en E

With plage

'crée les bordures
.Borders(xlLeft).LineStyle = xlContinuous
.Borders(xlRight).LineStyle = xlContinuous
.Borders(xlTop).LineStyle = xlContinuous
.Borders(xlBottom).LineStyle = xlContinuous
'.Interior.ColorIndex = 43

'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 = 34

'MFC 3ème condition
.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(LIGNE();1)=0"
.FormatConditions(3).Interior.ColorIndex = 36

End With


Application.Calculation = xlAutomatic
Application.DisplayAlerts = True

End Sub
 
- 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
2
Affichages
142
Réponses
4
Affichages
197
Réponses
2
Affichages
178
Retour