-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-in-excel-mergen.vb
70 lines (62 loc) · 2.59 KB
/
csv-in-excel-mergen.vb
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
Sub ImportCSVFromFolder()
Dim wsTemp As Worksheet, wsTarget As Worksheet, curCell As Range, CSVPFAD As String, fso As Object, f As Object, strCSVDelimiter As String
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "C:\Users\Evgeniy.MEDIAWORXDE\Downloads\1"
.Title = "Ordnerauswahl"
.ButtonName = "Auswahl..."
.InitialView = msoFileDialogViewList
If .Show = -1 Then
CSVPFAD = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Legt das CSV-Trennzeichen für die Dateien fest
strCSVDelimiter = ";"
Set fso = CreateObject("Scripting.Filesystemobject")
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Zielarbeitsblatt für die importierten Daten
Set wsTarget = Worksheets(1)
wsTarget.Name = "Zusammenfassung"
'temporäres Arbeitsblatt für den Import der Daten erstellen
Set wsTemp = Worksheets.Add(After:=Worksheets(Worksheets.Count))
'Inhalt des Zusammenfassungsblattes löschen
wsTarget.UsedRange.Clear
'Startausgabezelle festlegen
Set curCell = wsTarget.Range("A1")
For Each f In fso.GetFolder(CSVPFAD).Files
If LCase(fso.GetExtensionName(f.Name)) = "csv" Then
'Temporäres Sheet löschen
wsTemp.UsedRange.Clear
'CSV-Daten in Temporäres Sheet importieren
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & f.Path, Destination:=wsTemp.Range("$A$1"))
.Name = "import"
.FieldNames = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileOtherDelimiter = strCSVDelimiter
.Refresh BackgroundQuery:=False
.Delete
End With
With wsTemp
'Daten in Zielsheet kopieren
.UsedRange.Copy curCell
End With
'Ausgabezeile eins nach unten schieben
Set curCell = wsTarget.Cells(wsTarget.UsedRange.Rows.Count + 2, 1)
End If
Next
'Temporäres Sheet löschen
wsTemp.Delete
'Spalten anpassen
wsTarget.Columns.AutoFit
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Vorgang beendet!", vbInformation
Set fso = Nothing
End Sub