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
| Option Explicit
Sub FilTest()
Call SetFiltValue(4, "v")
'Attention: il faut insérer une ligne après la 3 (Train vapeur)
End Sub
Sub SetFiltValue(Optional ColVal As Integer, Optional FiltVal As Variant)
Dim InpRng As Range, FiltRng As Range
Call Remove_Autofilter(Worksheets("Source"))
Set InpRng = Worksheets("Source").Range("C5").CurrentRegion
Debug.Print InpRng.Address
If ColVal > 0 And Not (IsMissing(FiltVal)) Then
InpRng.AutoFilter Field:=ColVal, Criteria1:=FiltVal
Else: InpRng.AutoFilter
End If
' On ne prend pas la ligne de titre et on récupère les cellules visibles après filtrage
Set FiltRng = InpRng.Offset(1, 0).Resize(InpRng.Rows.Count - 1, InpRng.Columns.Count).SpecialCells(xlCellTypeVisible)
Debug.Print FiltRng.Address, InpRng.Address
FiltRng.Copy Destination:=Worksheets("Final").Range("A1")
' Si l'on veut supprimer les enregistrements de la feuille d'origine (non testé)
FiltRng.EntireRow.Delete
End Sub
Sub Remove_Autofilter(Optional Wsh As Worksheet)
If IsMissing(Wsh) Then Set Wsh = ActiveSheet
If Wsh.AutoFilterMode = True Then Wsh.AutoFilterMode = False
End Sub |
Partager