Thema Datum  Von Nutzer Rating
Antwort
18.02.2019 14:33:12 HerrMoartl
NotSolved
18.02.2019 14:36:40 HerrMoartl
NotSolved
19.02.2019 08:44:26 Gast98539
NotSolved
19.02.2019 10:06:39 Gast464
NotSolved
19.02.2019 12:09:49 Gast41783
NotSolved
19.02.2019 12:45:56 Gast98539
NotSolved
Rot Crossposting
19.02.2019 20:01:02 Gast47205
NotSolved
20.02.2019 09:00:47 Gast61860
NotSolved

Ansicht des Beitrags:
Von:
Gast47205
Datum:
19.02.2019 20:01:02
Views:
629
Rating: Antwort:
  Ja
Thema:
Crossposting

Wie ich sehe hat sich noch keiner dazu gemeldet - auch verständlich: Das ist schon keine simple Frage mehr (es ist nicht mal eine Frage!!).

 

Wie dem auch sei, ich zeige hier mal eine Variante auf, die Gebrauch von einigen Features macht (so z.B. Power Query):

Nur damit das klar ist: ich antworte hier nicht auf den Themenersteller, sondern ich antworte hier der VBA-Community. Das Thema ist durchaus interessant, da man hier stark unterschiedliche Wege gehen kann, um schlussendlich ans gleiche Ziel zu gelangen. Der nachfolgende Weg ist vermutlich nicht gerade derjenige, den viele kennen.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'Modul: modImport
Option Explicit
 
Public Sub ImportCSV_Special(ByVal FilePath As String, ByVal Destination As Object)
   
'---------------------------------------------
  Const C_VALUE_FORMAT  As String = "0.00"
  Const C_DATE_FORMAT   As String = "m/d/yyyy"
  Const C_TIME_FORMAT   As String = "[$-F400]h:mm:ss AM/PM"
'---------------------------------------------
   
  FilePath = Trim$(FilePath)
   
  If Dir$(FilePath) = "" Then
     
    Call MsgBox("CSV-Datei '" & FilePath & "' konnte nicht gefunden werden.", vbExclamation)
    Exit Sub
   
  End If
   
  Dim rngDest     As Excel.Range
  Dim objQuery    As WorkbookQuery
   
  'destination for csv data
  If Destination Is Nothing Then
     
    Call MsgBox("Kein Zielort für Import der CSV-Datei '" & FilePath & "' vorhanden.", vbExclamation)
    Exit Sub
     
  ElseIf TypeOf Destination Is Excel.Worksheet Then
    Set rngDest = Destination.Range("A1")
  ElseIf TypeOf Destination Is Excel.Range Then
    Set rngDest = Destination.Cells(1, 1)
  Else
     
    Call MsgBox("Der Zielort für Import der CSV-Datei '" & FilePath & "' ist ungültig.", vbExclamation)
    Exit Sub
     
  End If
 
'## initialize data source connections ##
 
  'create Power Query to data (csv file)
  Set objQuery = ThisWorkbook.Queries.Add("CSV-Request-" & Format$(Now, "yyyymmddhhnnss"), _
    "let" & vbNewLine & _
      "#""CSV-Src"" = Csv.Document(File.Contents(""" & FilePath & """),[Delimiter="";"", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None])," & vbNewLine & _
      "#""CSV-Src-T"" = Table.TransformColumnTypes(#""CSV-Src"",{{""Column1"", type text}, {""Column2"", type number}, {""Column3"", type datetime}})," & vbNewLine & _
      "#""CSV-Source"" = Table.RenameColumns(#""CSV-Src-T"",{{""Column1"", ""Name""}, {""Column2"", ""Wert""}, {""Column3"", ""Datum""}})" & vbNewLine & _
    "in" & vbNewLine & _
      "#""CSV-Source""")
   
  Dim objCon As WorkbookConnection
  Dim pvt As Excel.PivotTable
   
  'connect to query
  Set objCon = ThisWorkbook.Connections.Add2(objQuery.Name & " - Connection", "", _
              "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & objQuery.Name & ";Extended Properties=""""", _
              "SELECT Name, (Wert / 1000000) As Wert, Datum FROM [" & objQuery.Name & "]", XlCmdType.xlCmdSql)
   
  With ThisWorkbook.Worksheets.Add
   
    'create pivot table via query-connection
    Set pvt = ThisWorkbook.PivotCaches.Create(xlExternal, objCon).CreatePivotTable(.Range("A1"), "CSV-Pivot")
     
    'set pivot view
    pvt.ColumnGrand = False
    pvt.RowGrand = False
    pvt.DisplayFieldCaptions = False
     
    'assign pivot fields
    pvt.PivotFields("Name").Orientation = XlPivotFieldOrientation.xlColumnField
    pvt.PivotFields("Wert").Orientation = XlPivotFieldOrientation.xlDataField
    pvt.PivotFields("Datum").Orientation = XlPivotFieldOrientation.xlRowField
     
    'set format for pivot data field (will be copied later)
    pvt.DataFields(1).NumberFormat = C_VALUE_FORMAT
     
    Dim rngData As Excel.Range
     
    'referencing pivot data range (including headers)
    Set rngData = pvt.RowRange.Resize(ColumnSize:=pvt.RowRange.Columns.Count + pvt.ColumnRange.Columns.Count)
     
    'clear destination first
    rngDest.CurrentRegion.Clear
    'copy pivot data to ...
    rngData.Copy rngDest
     
    Application.DisplayAlerts = False
    'delete worksheet (incl. pivot table)
    .Delete
    Application.DisplayAlerts = True
     
  End With
   
  'delete connection to query
  If Not objCon Is Nothing Then objCon.Delete
  'delete query
  If Not objQuery Is Nothing Then objQuery.Delete
   
'## alter & format output ##
   
  rngDest.Parent.Activate
   
  Set rngDest = rngDest.CurrentRegion
   
  'DateTime -> Date | Time
  rngDest.Columns(2).Resize(, 2).Insert xlShiftToRight
  rngDest.Rows(1).Resize(, 3).Value = Array("", "Datum", "Uhrzeit")
   
  'referencing data rows
  With rngDest.Rows.Offset(1).Resize(rngDest.CurrentRegion.Rows.Count - 1)
     
    'column: Date
    .Columns(2).NumberFormat = C_DATE_FORMAT
    .Columns(2).Formula = "=DATE(YEAR(RC[-1]),MONTH(RC[-1]),DAY(RC[-1]))"
    .Columns(2).Value = .Columns(2).Value
     
    'column: Time
    .Columns(3).NumberFormat = C_TIME_FORMAT
    .Columns(3).Formula = "=TIME(HOUR(RC[-2]),MINUTE(RC[-2]),SECOND(RC[-2]))"
    .Columns(3).Value = .Columns(3).Value
     
    'delete column: DateTime
    .Columns(1).EntireColumn.Delete
     
    .EntireColumn.AutoFit
  End With
   
  Call MsgBox("Import von '" & FilePath & "' erfolgreich abgeschlossen.", vbInformation)
   
End Sub

 

Aufgerufen wird das dann also einfach mit:

1
ImportCSV_Special "D:\data.csv", ThisWorkbook.Worksheets("Tabelle1").Range("A1")

 

Viele Grüße an die VBA Community


Ihre Antwort
  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen
Thema: Name: Email:



  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen

Thema Datum  Von Nutzer Rating
Antwort
18.02.2019 14:33:12 HerrMoartl
NotSolved
18.02.2019 14:36:40 HerrMoartl
NotSolved
19.02.2019 08:44:26 Gast98539
NotSolved
19.02.2019 10:06:39 Gast464
NotSolved
19.02.2019 12:09:49 Gast41783
NotSolved
19.02.2019 12:45:56 Gast98539
NotSolved
Rot Crossposting
19.02.2019 20:01:02 Gast47205
NotSolved
20.02.2019 09:00:47 Gast61860
NotSolved