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 62 63 64 65
| Option Explicit
Sub Reprendre()
Dim fd As Office.FileDialog, sFile As String
Dim wbData As Workbook, wshData As Worksheet, rData As Range, sData As String
Dim kEtu As Long, kExe As Long, kMar As Long, nR As Long
Dim kEtuD As Long, kExeD As Long, kMarD As Long, nRD As Long
Dim sFml As String
'--- recherche n° colonnes dans feuille en cours
kEtu = Application.WorksheetFunction.Match("Etude", Range("1:1"), 0)
kExe = Application.WorksheetFunction.Match("Exe", Range("1:1"), 0)
kMar = Application.WorksheetFunction.Match("Marches", Range("1:1"), 0)
nR = Range("A" & Rows.Count).End(xlUp).Row
nR = nR - 1 '--- dernière ligne n'est pas une donnée
Debug.Print kEtu, kExe, kMar, nR '--- pour info
'--- sélection fichier antérieur
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Filters.Add "Fichiers Excel", "*.xls*", 1
.Title = "Choisir un fichier Excel"
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.Path
If .Show = True Then
sFile = .SelectedItems(1)
Debug.Print sFile
Set wbData = Application.Workbooks.Open(sFile)
Set wshData = wbData.Worksheets(1)
Set fd = Nothing
Else
MsgBox "Annulé", , "Pour info"
Set fd = Nothing
Exit Sub
End If
End With
'--- recherche n° colonnes dans wshData
kEtuD = Application.WorksheetFunction.Match("Etude", wshData.Range("1:1"), 0)
kExeD = Application.WorksheetFunction.Match("Exe", wshData.Range("1:1"), 0)
kMarD = Application.WorksheetFunction.Match("Marches", wshData.Range("1:1"), 0)
Debug.Print kEtuD, kExeD, kMarD
nRD = wshData.Range("A" & wshData.Rows.Count).End(xlUp).Row
nRD = nRD - 1 '--- la dernière ligne n'est pas une donnée
'--- dénomination de la plage de recherche
Set rData = wshData.Range(wshData.Cells(2, 1), wshData.Cells(nRD, Application.WorksheetFunction.Max(kEtuD, kExeD, kMarD)))
Debug.Print wbData.Path, wbData.Name, wshData.Name, rData.Address
sData = "'" & wbData.Path & "\[" & wbData.Name & "]" & wshData.Name & "'!" & rData.Address
Debug.Print sData
wbData.Close
'--- inscription formules
sFml = "=IFERROR(VLOOKUP($A2," & sData & ", 000, FALSE),"""")"
Cells(2, kEtu).Formula = Replace(sFml, "000", kEtuD)
Cells(2, kEtu).Copy
Range(Cells(2, kEtu), Cells(nR, kEtu)).PasteSpecial xlPasteFormulas
Cells(2, kExe).Formula = Replace(sFml, "000", kExeD)
Cells(2, kExe).Copy
Range(Cells(2, kExe), Cells(nR, kExe)).PasteSpecial xlPasteFormulas
Cells(2, kMar).Formula = Replace(sFml, "000", kMarD)
Cells(2, kMar).Copy
Range(Cells(2, kMar), Cells(nR, kMar)).PasteSpecial xlPasteFormulas
'--- cloture
Set rData = Nothing
Set wshData = Nothing
Set wbData = Nothing
MsgBox "Formules inscrites.", , "Pour info"
End Sub |
Partager