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 58 59 60 61
| Private Sub CBTN_Click()
Dim Chemin As String, Fichier As String, col1 As String, col2 As String
Dim Wb As Workbook
Dim rep As Variant, Folder As Variant
Dim NBFichier As Integer
'Définit le répertoire contenant les fichiers
Chemin = ThisWorkbook.Path & Application.PathSeparator
Folder = "extract_SAP\"
'Tous les fichiers xlsx du répertoire
Fichier = Dir(Chemin & Folder & CBTN.Caption & "*.xlsx")
While Not Fichier = ""
NBFichier = NBFichier + 1
Fichier = Dir
Wend
rep = MsgBox("Voulez-vous continuer et importer" & " " & NBFichier & " " & "fichiers" & " " & "pour" & " " & CBTN.Caption & " ? ", vbYesNo + vbQuestion + vbApplicationModal + _
vbDefaultButton2, "")
col1 = Application.WorksheetFunction.VLookup(CBTN.Caption, Sheets("setup").Range("A3:C14"), 2, 0)
col2 = Application.WorksheetFunction.VLookup(CBTN.Caption, Sheets("setup").Range("A3:C14"), 3, 0)
Fichier = Dir(Chemin & Folder & CBTN.Caption & "*.xlsx")
Application.ScreenUpdating = False
If rep = vbYes Then
If Fichier <> "" Then
Sheets("MOIS-MAAND").Range(col1 & "2" & ":" & col2 & ActiveSheet.UsedRange.Rows.Count - 1).ClearContents
Do While Fichier <> ""
'Désactive l'évènement
Application.EnableEvents = False
' ouvre fichier trouvé
Set Wb = Workbooks.Open(Chemin & Folder & Fichier)
' copy/paste les données en valeurs
Range("A2:C" & ActiveSheet.UsedRange.Rows.Count).Copy
ThisWorkbook.Sheets("MOIS-MAAND").Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ClearClipboard
' ferme le fichier trouvé en cours
Wb.Close True
Set Wb = Nothing
Fichier = Dir
Loop
MsgBox "Les données sont importées", vbOKOnly + vbInformation, ""
Sheets("MOIS-MAAND").Range(col1 & "1").Activate
'Réactive l'évènement
Application.EnableEvents = True
'Sauve les données importées
ThisWorkbook.Save
Else
MsgBox "Un ou plusieurs fichiers n'existent pas"
End If
Else
End If
Application.ScreenUpdating = True
ThisWorkbook.Save
End Sub |
Partager