AW: Daten aus mehreren Dateien kopieren
09.02.2019 10:44:13
Roman
Hallo Piet, bin jetzt zwar nicht in der Arbeit und habe somit nicht den angepassten Code, aber das war folgender Code. Hatte es auch versucht bei With .Cells... mit .NumberFormat = "@", dann wird nur der Dateipfad in alle Zellen eingetragen.
Übrigens, bei diesem Code benötige ich nicht, dass der Dateipfad ausgelesen und kopiert/eingefügt wird, habe es aber nicht geschafft ihn dementsprechend anzupassen.
Ich schildere einfach genau, was ich haben möchte: Ich habe einen Ordner mit vielen Dateien, die gleich aufgebaut sind. Nun soll in einer neuen Datei ab Zeile 4 und Spalte 1 mehrere Zellen aus den vielen Dateien aufgelistet werden z. B. die Zellen B3, D3, F3, B6, D6, F6 (der Dateipfad der vielen Dateien soll nicht kopiert/eingefügt werden). Die Formate der kopierten Inhalte sollen dabei erhalten bleiben. Unterordner sollte ich per true / false bestimmen können.
Danke schon mal im Voraus.
Option Explicit
Const strSheetQ As String = "Sheet1" ' Die Tabelle wird ausgelesen
Const strSheetZ As String = "Sheet1" ' Die Tabelle in dieser Datei
Const strCellQ1 As String = "C3" ' Diese Zellen werden ausgelesen
Const strCellQ2 As String = "C6"
Const strCellQ3 As String = "A9"
Const strCellQ4 As String = "B10"
Const strCellQ5 As String = "D11"
Public Sub Files_Read()
Dim stCalc As XlCalculationState
Dim strDir As String
Dim objFSO As Object
Dim objDir As Object
On Error GoTo Fin
With Application
.ScreenUpdating = False
.AskToUpdateLinks = False
.EnableEvents = False
stCalc = .Calculation
.Calculation = xlCalculationManual
.DisplayAlerts = False
End With
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDir = ThisWorkbook.Path ' Datei im gleichen Ordner wie Auswertungsdateien
Set objDir = objFSO.GetFolder(strDir)
'dirInfo objDir, "*.xls", True ' Mit Unterordner
dirInfo objDir, "*.xls"
Fin:
With Application
.ScreenUpdating = True
.AskToUpdateLinks = True
.EnableEvents = True
.Calculation = stCalc
.DisplayAlerts = True
End With
Set objDir = Nothing
Set objFSO = Nothing
End Sub
Public Sub dirInfo(ByVal objCurrentDir As Object, ByVal strName As String, _
Optional ByVal blnTMP As Boolean = False)
Dim objWorkbook As Workbook
Dim strFormula As String
Dim lngLastRow As Long
Dim varTMP As Variant
For Each varTMP In objCurrentDir.Files
If varTMP.Name Like strName And varTMP.Name ThisWorkbook.Name Then
With ThisWorkbook.Worksheets(strSheetZ)
lngLastRow = IIf(Len(.Cells(.Rows.Count, 2)), _
.Rows.Count, .Cells(.Rows.Count, 2).End(xlUp).Row) + 1
With .Cells(lngLastRow, 2)
.Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, "\") + 1) & "]" & _
strSheetQ & "'!" & strCellQ1
.Offset(0, -1).Value = varTMP.Name
End With
With .Cells(lngLastRow, 3)
.Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, "\") + 1) & "]" & _
strSheetQ & "'!" & strCellQ2
End With
With .Cells(lngLastRow, 4)
.Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, "\") + 1) & "]" & _
strSheetQ & "'!" & strCellQ3
End With
With .Cells(lngLastRow, 5)
.Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, "\") + 1) & "]" & _
strSheetQ & "'!" & strCellQ4
End With
With .Cells(lngLastRow, 6)
.Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, "\") + 1) & "]" & _
strSheetQ & "'!" & strCellQ5
End With
.UsedRange.Value = .UsedRange.Value
End With
End If
Next varTMP
If blnTMP = True Then
For Each varTMP In objCurrentDir.SubFolders
dirInfo varTMP, strName
Next varTMP
End If
Set objWorkbook = Nothing
End Sub