Simplification de macro

mmaatthh

XLDnaute Nouveau
Bonjour le forum
Dans une feuille de calcule chaque cellule est remplie soit par la lettre J,S OU N... Mais il y a des combinaison qui sont indésirable ppour les trouver Voici une macro qui rougie les bordures des cellules "J" si la cellule qui la précède est "N" ou "S". Comme j'ai plusieurs critères autre que "N" ou "S" j'aimerai pouvoir simplifier l'écriture de la macro. Peut-être en créant des variable? Je ne le sais pas! De plus il y a le problème des majuscules minuscules. Si la cellule précédente est petit "n" ou petit "s", la macro ne fonctionne plus.

Merci beaucoup pour votre aide

Sub Audit()
ActiveSheet.Unprotect
Dim acell As Object
Range(Selection.Address).Select
For Each acell In Selection
'JOUR
If acell = "J" And acell.Offset(0, -1) = "N" Then
With acell.Borders(xlEdgeLeft)
.ColorIndex = 3
End With
With acell.Borders(xlEdgeTop)
.ColorIndex = 3
End With
With acell.Borders(xlEdgeBottom)
.ColorIndex = 3
End With
With acell.Borders(xlEdgeRight)
.ColorIndex = 3
End With
End If
If acell = "j" And acell.Offset(0, -1) = "N" Then
With acell.Borders(xlEdgeLeft)
.ColorIndex = 3
End With
With acell.Borders(xlEdgeTop)
.ColorIndex = 3
End With
With acell.Borders(xlEdgeBottom)
.ColorIndex = 3
End With
With acell.Borders(xlEdgeRight)
.ColorIndex = 3
End With
End If
Next
Range("B3").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
 

kiki29

XLDnaute Barbatruc
Re : Simplification de macro

Déjà pour simplifier
Code:
.....   
For Each acell In Selection
        If UCase(acell) = "J" And UCase(acell.Offset(0, -1)) = "N" Then
            With acell.Borders(xlEdgeLeft)
                .ColorIndex = 3
            End With
            With acell.Borders(xlEdgeTop)
                .ColorIndex = 3
            End With
            With acell.Borders(xlEdgeBottom)
                .ColorIndex = 3
            End With
            With acell.Borders(xlEdgeRight)
                .ColorIndex = 3
            End With
        End If
Next
....
 
Dernière édition:

mmaatthh

XLDnaute Nouveau
Re : Simplification de macro

Merci kiki
Mon problème de majuscule, minuscule est réglé. Je me demandais s'il était possible d'écrire la macro sans répéter le tout 2 fois. Comment formuler si la cellule = J et la précédente soit S ou soit N alors les bortures sont rouge?
Merci MMaatthh
 

Dan

XLDnaute Barbatruc
Re : Simplification de macro

bonjour,

tu peux aussi mettre :

Code:
.....
For Each acell In Selection

[LEFT]      If UCase(acell) = "J" And UCase(acell.Offset(0, -1)) = "N" Or UCase(acell.Offset(0, -1)) = "S" Then
[LEFT]         acell.Borders.ColorIndex = 3
      End If
Next[/LEFT]
....



A bientôt

[/LEFT]
 

mmaatthh

XLDnaute Nouveau
Re : Simplification de macro

Merci Dan
Pour avoir exactement ce que je voilais j'ai modifier le code un peu pour :
For Each acell In Selection
If UCase(acell) = "J" And UCase(acell.Offset(0, -1)) = "N" Or UCase(acell) = "J" And UCase(acell.Offset(0, -1)) = "S" Then
acell.Borders.ColorIndex = 3
End If

Le tout marche très bien
 

Statistiques des forums

Discussions
312 579
Messages
2 089 876
Membres
104 298
dernier inscrit
MarieCB