Importer des données avec VBA
Bonjour,
J'aimerais créer une macro qui permette à l’utilisateur de sélectionner un fichier Excel et copier les données du fichier sélectionné vers une feuille non vide du fichier dans lequel se trouve la macro. J'ai trouvé cette procédure sur le forum que j'ai un peu modifié et elle bug maintenant
Pourriez vous m'aider svp ?
Cordialement
Code:
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
| Sub Importer_Donnees()
'IMPORTATION D'UNE SEULE FEUILLE DE DONNEES
Dim DestBook As Workbook, SourceBook As Workbook
Dim DestSheet As Worksheet
Dim RetVal As Boolean
' Turn off screen updating.
Application.ScreenUpdating = False
'chemin en relatif du dossier ou se trouve tes données
ChDir ("C\")
'ou en hard
'Chemin = "C:\...\dossier"
MonFichier = Application.GetOpenFilename(FileFilter:="Fichiers Excel (*.xl*), *.xl*")
' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestSheet = Sheets(1) 'active la cellule OU TU AIMERAIS COPIER LES DONNEES
' Show the Open dialog box.
RetVal = Application.Dialogs(xlDialogOpen).Show(Chemin & "\*.*")
' If Retval is false (Open dialog canceled), exit the procedure.
If RetVal = False Then Exit Sub
' Set an object variable for the workbook containing the text file.
Set SourceBook = ActiveWorkbook
' Copy the contents of the entire sheet containing the text file.
Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy
'OU copie un range défini
'Range("A1:AD2000").copy
' Activate the destination workbook and paste special the values
' from the text file.
DestBook.Activate
DestCell.PasteSpecial Paste:=xlValues
' Close the book containing the text file.
SourceBook.Close False
Application.ScreenUpdating = True
End Sub |