AW: aus geschlossenen Dateien Daten kumulieren
13.11.2018 14:58:15
Aleks
Habe es jetzt alles per Makro einfügen können und habe dann einen extra Sheet mit den Bezügen gemacht um dann quasi die Daten die ich importiere direkt da einzufügen. Das ist der Code (nur zur info):
Function GetConnXLS(ByVal cFileName As String, _
Optional ByVal InformErrMSG As Boolean = False) As ADODB.Connection
'On Error GoTo LOI:
'Open ADO connection to excel workbook
Dim oConn As ADODB.Connection
Dim Ext As String, ConnStr As String
Set oConn = New ADODB.Connection
ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & cFileName & ";" & _
"Extended Properties=""Excel 12.0 xml;HDR=Yes"";"
oConn.Open ConnStr
Set GetConnXLS = oConn
Exit Function
LOI:
If Err.Number 0 Then
Set oConn = Nothing
If InformErrMSG Then
MsgBox "GetConnXLS" & ": " & Err.Number & " " & Err.Description, vbCritical
End If
End If
End Function
Sub Merge_All()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sh As Worksheet
Dim I As Long, k As Long, CountFiles As Long, J As Long, strData, _
kDS As Long, xKorr As Integer
files = Application.GetOpenFilename(, , , , True)
If VarType(files) = vbBoolean Then Exit Sub
Set sh = Sheets("Sheet1")
For k = LBound(files) To UBound(files)
'Anzahl der Datensätze in der ausgewählten Datei ermitteln
kDS = lastRowClosedFile(files(k), "Cockpit", "A:A")
'ADODB-Connection erstellen
Set cnn = GetConnXLS(files(k))
If cnn Is Nothing Then
MsgBox "Check lai co so du lieu file: " & files(k)
Exit Sub
End If
'Select-Befehl zusammenstellen (quasi welche Daten ausgewählt werden sollen)
strData = "SELECT * From [Valuation act$A1:CC10" & kDS & "];"
'Recordset öffnen auf der Grundlage der Connection & Select-Befehl
Set rst = cnn.Execute(strData)
CountFiles = CountFiles + 1
If CountFiles = 1 Then
For J = 0 To rst.Fields.Count - 1
sh.Cells(3, J + 1).Value = rst.Fields(J).Name
Next J
End If
If k = 1 Then
xKorr = 1
Else
xKorr = 0
End If
sh.Range("I" & 4 + I - xKorr).Value = files(k)
I = I + sh.Range("A" & 4 + I).CopyFromRecordset(rst)
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing
Next k
MsgBox "Done", vbSystemModal + 48, "Nur das Beste für unsere Gf"
End Sub