Bonjour cousinhub,
Merci pour ta réponse.
Voici ma macro d'importation :
Sub Copy()
Dim dl1 As Long, dl2 As Long, dl3 As Long 'déclaration des dernières lignes
Dim wb1 As Workbook, wb2 As Workbook 'déclaration des classeurs
Dim ws1 As Worksheet, ws2 As Worksheet 'déclaration des feuilles
If MsgBox("blabla" _
& Chr(10) & "Ce fichier se trouve dans le dossier : P:\blabla\Données brutes\", vbInformation + vbOKCancel, "Convertion") = vbCancel Then
Exit Sub
Else:
On Error GoTo Erreur 'si erreur, va directement à Erreur en fin de code
ChDrive "P"
ChDir "P:\blabla\Extraction\" 'chemin par défaut
Set wb1 = ThisWorkbook 'définition du classeur 1 (ce classeur)
Set ws1 = wb1.Sheets("Import") 'définition de la feuille 1 (Import de ce classeur)
Set wb2 = Workbooks.Open(Application.GetOpenFilename) 'définition du classeur 2 (classeur à importer)
Set ws2 = wb2.Worksheets(1) 'définition de la feuille 2 (1er feuille du classeur à importer)
dl1 = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
dl2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
If MsgBox("blabla ?", vbQuestion + vbYesNo, "Importation") = vbNo Then
wb2.Close False
Exit Sub
Else:
Application.ScreenUpdating = False
ws1.Unprotect "mdp"
ws2.Range("A3:I" & dl2).Copy
ws1.Range("A" & dl1).PasteSpecial Paste:=xlPasteValues
wb2.Close False
With ws1
dl3 = ws1.Range("A" & Rows.Count).End(xlUp).Row
.Range("A11:A" & dl3).NumberFormat = "@"
.Range("B11:B" & dl3).NumberFormat = "dd/mm/yyyy"
.Range("C11:C" & dl3).NumberFormat = "@"
.Range("D11:D" & dl3).NumberFormat = "@"
.Range("E11:E" & dl3).NumberFormat = "@"
.Range("F11:F" & dl3).NumberFormat = "@"
.Range("G11:G" & dl3).NumberFormat = "@"
.Range("H11:H" & dl3).NumberFormat = "@"
.Range("I11:I" & dl3).NumberFormat = "@"
.Range("A11:I" & dl3).WrapText = True
.Range("A11:I" & dl3).HorizontalAlignment = xlCenter
.Range("A11:I" & dl3).VerticalAlignment = xlCenter
.Range("A11:I" & dl3).EntireRow.AutoFit
.Range("A11:I" & dl3).Borders.Value = 1
.Range("A11:I" & dl3).Borders(xlEdgeLeft).Weight = xlThick
.Range("A11:I" & dl3).Borders(xlEdgeRight).Weight = xlThick
.Range("A11:I" & dl3).Borders(xlEdgeTop).Weight = xlMedium
.Range("A11:I" & dl3).Borders(xlEdgeBottom).Weight = xlThick
.Range("A11:I" & dl3).Sort key1:=Range("A11"), Order1:=xlAscending
.Range("A11:I" & dl3).Locked = True
.Protect "mdp", True, True, False, AllowFormattingCells:=False, _
AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End With
End If
End If
Application.ScreenUpdating = True
MsgBox "blabla !", vbInformation, "Importation réussie !"
Exit Sub 'Permet de sortir de la procédure et évite la gestion d'erreur (Erreur), si la macro
's'est déroulée sans encombre.
Erreur:
MsgBox "Aucun fichier sélectionné !", vbExclamation, "Annulation !"
ws1.Protect "mdp", True, True, False, AllowFormattingCells:=False, _
AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End Sub