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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| Private Sub CommandButton1_Click()
If Me.TextBox1.Text = "" Then
MsgBox "Veuillez indiquer votre formation, merci."
Me.TextBox1.SetFocus
Exit Sub
End If
'création d'une feuille pour y copier les données
Dim sh As Worksheet
Set sh = Worksheets.Add
sh.Name = TextBox1
' Rechercher dans la liste, Rapport1 voulu
Dim LastLign As Long ' Dernière ligne de la liste dans la feuille Rapport1
Dim Pcellule As Range 'Première cellule trouver
Dim Num As String 'Code d'identification
Dim WbActif As Workbook
Dim WbFiltre As Workbook
Set WbActif = ActiveWorkbook
Set WbFiltre = Workbooks.Add
WbFiltre.Sheets(Rapport1).Range("A1") = sh.Range("A1")
LastLign = WbActif.Worksheets("Rapport1").UsedRange.Rows.count + 1
FiltreActif sh.UsedRange, WbFiltre.Sheets(Rapport1).UsedRange, WbActif.Worksheets("Rapport1").Cells(LastLign, 1), False
WbFiltre.Close False
WbActif.Worksheets("Rapport1").Cells(LastLign, 1).EntireRow.Delete
NewLign = 2
Num = TextBox1
With Worksheets("Rapport1").Range("B1:B" & LastLign)
Set c = .Find(Num)
Set Pcellule = c 'enregistre le premier élémént trouver
If Not c Is Nothing Then
Do
c.EntireRow.copy Destination:=Worksheets(TextBox1).Range("B" & NewLign) 'copie la ligne dans l'autre feuille
Set c = .FindNext(c) ' recherche si il y a un autre code dans la liste
NewLign = NewLign + 1
Loop While Not c Is Nothing And c <> Pcellule
End If
End With
End Sub
Function FiltreActif(RangeSource As Range, CriterRange As Range, CopyRange As Range, Optional Unique As Boolean = True) As Boolean
FiltreActif = False
On Error Resume Next
RangeSource.AdvancedFilter Action:= _
xlFilterCopy, CriteriaRange:=CriterRange _
, CopyToRange:=CopyRange, Unique:=Unique
DoEvents
If Err = 0 Then FiltreActif = True
MsgBox Err.Description
On Error GoTo 0
End Function |
Partager