Het is gelukt, bedankt voor de tips!
Met dit stukje code uit de gehele code pak ik de tekst uit de cel en gebruik ik die in de formule:
code:
1
| Cells(13 + (X - 1) * 6, 2).Value = "='" & fPath & "[" & fileList(X) & "]" & fSheet & "'!" & fCell |
fPath is een variabele waarin de bestandsmap staat
fileList (X) is de naam van het bestand
fSheet is de naam van de worksheet uit het bestand waaruit die de cellen pakt
fCell is de cel
Nadat de formule in de eerste cel staat kopieer ik dit door naar de rest van de cellen m.b.v. de codes die erop volgen
Hier is de hele code die ik heb gebruikt:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| Sub VerzamelOffertes()
Dim fileList() As String
Dim fName As String
Dim fPath As String
Dim fSheet As String
Dim i As Integer, X As Integer
fPath = Range("B1").Value
fName = Dir(fPath & "*.xls*")
fSheet = Range("M1").Value
fCell = Range("R1").Value
Application.ScreenUpdating = False
Rows("12:9999").Delete Shift:=xlUp
While fName <> ""
i = i + 1
ReDim Preserve fileList(1 To i)
fileList(i) = fName
fName = Dir()
Wend
If i = 0 Then
MsgBox "Geen bestanden gevonden"
Exit Sub
Else
For X = 1 To i
'maak lijst van offertenamen
Cells(13 + (X - 1) * 6, 1) = fileList(X)
'zet formule in de cel rechts naast de offertenaam
Cells(13 + (X - 1) * 6, 2).Value = "='" & fPath & "[" & fileList(X) & "]" & fSheet & "'!" & fCell
'kopieer cel naar rechts
Cells(13 + (X - 1) * 6, 2).AutoFill Destination:=Range(Cells(13 + (X - 1) * 6, 2), Cells(13 + (X - 1) * 6, 53)), Type:=xlFillDefault
'kopieer hele rij naar onder
Range(Cells(13 + (X - 1) * 6, 2), Cells(13 + (X - 1) * 6, 53)).AutoFill Destination:=Range(Cells(13 + (X - 1) * 6, 2), Cells(17 + (X - 1) * 6, 53)), Type:=xlFillDefault
'kopieer opmaak van samenvattende tabel
Range("B6:BA10").Copy
Cells(13 + (X - 1) * 6, 2).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
'maak blok van titel
Range(Cells(13 + (X - 1) * 6, 1), Cells(17 + (X - 1) * 6, 1)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range("A1").Select
Next
End If
Application.CalculateFull
Range("A13").Select
Application.ScreenUpdating = True
End Sub |