Simplifier une macro

dirmon

XLDnaute Junior
Bonjour à tous,

Comme d'habitude, je m'adresse à cet excellent forum pour trouver une réponse à mon problème.

J'ai sur plusieurs feuilles la macro suivante :

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Application.ScreenUpdating = False
If Target.Address = "$A$3" Then
Range("C1:IV1").EntireColumn.Hidden = False
If Target.Value <> "" Then
For i = 3 To 120
If Cells(1, i).Value <> UCase(Target.Value) Then
Cells(1, i).EntireColumn.Hidden = True
End If
Next i
End If
End If
If Target.Address = "$A$5" Then
Range("C2:IV2").EntireColumn.Hidden = False
If Target.Value <> "" Then
For i = 3 To 120
If Cells(2, i).Value <> UCase(Target.Value) Then
Cells(2, i).EntireColumn.Hidden = True
End If
Next i
End If
End If
If Target.Address = "$A$1" Then
Range("IV:IV").EntireRow.Hidden = False
If Target.Value <> "" Then
For i = 7 To 682
If Cells(i, 256).Value <> UCase(Target.Value) Then
Cells(i, 256).EntireRow.Hidden = True
End If
Next i
End If
End If
Application.ScreenUpdating = True
End Sub


Cette macro se lance grace à trois cellules qui sont sous forme de validation.

Elles servent à modifier l'affichage de la feuille afin d'imprimer une partie des éléments.

Lors l'activation de cette macro,surtout pour la case A1,le traitement est très long.

Pouvez vous m'aider à la simplifier ?

Merci pour votre aide

Dirmon
 

JNP

XLDnaute Barbatruc
Re : Simplifier une macro

Bonjour Dirmon :),
Essaie de modifier ton code ainsi
Code:
    For i = 7 To 682
        If Cells(i, 256).Value <> UCase(Target.Value) Then
            If MaSélection Is Nothing Then
            Set MaSélection = Cells(i, 256)
            Else
            Set MaSélection = Union(MaSélection, Cells(i, 256))
            End If
        End If
    Next i
    MaSélection.EntireRow.Hidden = True
    Set MaSélection = Nothing
Je pense que ça devrait aller plus vite.
A adapter pour tes 2 autres tests.
Bon courage :cool:
 

dirmon

XLDnaute Junior
Re : Simplifier une macro

Bonjour JNP

Merci pour ta réponse mais l'adaptation me donne un message "Erreur d'execution 424objet requis"

Ci-joint mon code si je t'ai bien compris

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Application.ScreenUpdating = False
If Target.Address = "$A$1" Then
Range("IV:IV").EntireRow.Hidden = False
If Target.Value <> "" Then
For i = 7 To 682
If Cells(i, 256).Value <> UCase(Target.Value) Then
If MaSélection Is Nothing Then
Set MaSélection = Cells(i, 256)
Else
Set MaSélection = Union(MaSélection, Cells(i, 256))
End If
End If
Next i
MaSélection.EntireRow.Hidden = True
Set MaSélection = Nothing
End if
Application.ScreenUpdating = True
End Sub

Merci encore pour ton aide

Dirmon
 

dirmon

XLDnaute Junior
Re : Simplifier une macro

Bonjour Risleure,

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Dim MaSélection
Application.ScreenUpdating = False
If Target.Address = "$A$1" Then
Range("IV:IV").EntireRow.Hidden = False
If Target.Value <> "" Then
For i = 7 To 682
If Cells(i, 256).Value <> UCase(Target.Value) Then
If MaSélection Is Nothing Then
Set MaSélection = Cells(i, 256)
Else
Set MaSélection = Union(MaSélection, Cells(i, 256))
End If
End If
Next i
MaSélection.EntireRow.Hidden = True
Set MaSélection = Nothing
End If
End If
Application.ScreenUpdating = True
End Sub

Toujours même problème merci pour ton aide

Dirmon
 

pierrejean

XLDnaute Barbatruc
Re : Simplifier une macro

Bonjour Dirmon, JNP, Risleure

Pour ne plus avoir d'Erreur

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Application.ScreenUpdating = False
If Target.Address = "$A$1" Then
Set MaSélection = Nothing
   Range("IV:IV").EntireRow.Hidden = False
   If Target.Value <> "" Then
      For i = 7 To 682
        If Cells(i, 256).Value <> UCase(Target.Value) Then
          If MaSélection Is Nothing Then
            Set MaSélection = Cells(i, 256)
          Else
            Set MaSélection = Union(MaSélection, Cells(i, 256))
          End If
        End If
      Next i
        MaSélection.EntireRow.Hidden = True
        Set MaSélection = Nothing
   End If
End If
Application.ScreenUpdating = True
End Sub

Quant au resultat de la macro , je ne me prononce pas
 

Discussions similaires

Statistiques des forums

Discussions
312 331
Messages
2 087 353
Membres
103 528
dernier inscrit
hplus