1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
Dim Ws As Worksheet
Dim LastLig As Long, Numligne As Long
Dim C As String, D As String, E As String, MD As String, MC As String, v As Range
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Feuil1")
C = .Range("H7") 'premier critère
D = .Range("H8") 'second criètre
E = .Range("H9") 'troisième critère
Set Ws = Workbooks("fichierB.xlsx").Worksheets("Ongletsource") 'Le fichier FichierB doit être ouvert (et dans la même instance)
With Ws
.AutoFilterMode = False
LastLig = .Cells(.Rows.Count, 1).End(xlUp).Row
If C <> "" Then .Range("A1:X" & LastLig).AutoFilter Field:=11, Criteria1:=C
If D <> "" Then .Range("A1:X" & LastLig).AutoFilter Field:=12, Criteria1:=D
If E <> "" Then .Range("A1:X" & LastLig).AutoFilter Field:=13, Criteria1:=E
If .Range("A1:A" & LastLig).SpecialCells(xlCellTypeVisible).Count > 1 Then
.Range("A2:A" & LastLig).SpecialCells(xlCellTypeVisible).Copy ThisWorkbook.Worksheets("Feuil1").Range("A2")
End If
' debut de mon code pour appliquer une condition en fonction du résultat de la cellule Ix de chaque ligne trouvé par le trie ci-dessus
Numligne = 1 'on débute à la seconde ligne
For Each v In Range("a2:a" & LastLig) 'on boucle sur la selection trouvée
If Numligne <= LastLig Then 'si la numéro de la ligne est inférieur ou égale au nombre de ligne total (lastlig) on continue
Numligne = Numligne + 1 'on incrémente de 1
If .Cells("I" & Numligne & ":I" & Numligne) < 0 Then 'on test si la cellule est inférieure à zéro, si c'est le cas on exécute ci-dessous sinon on va au else
MD = -.Cells("I" & Numligne & ":I" & Numligne).SpecialCells(xlCellTypeVisible).Value
Cells(MD).Copy ThisWorkbook.Worksheets("Feuil1").Range("H13") ' Gauche
Else
MC = .Cells("I" & Numligne & ":I" & Numligne).SpecialCells(xlCellTypeVisible).Value
Cells(MC).Copy ThisWorkbook.Worksheets("Feuill1").Range("I13") ' Droite
End If
End If
Next v
' la suite de ton code
.AutoFilterMode = False
End With
Set Ws = Nothing
End With
End Sub |
Partager