Captain here,
Ich hab da ein Script, das mir alle .csv dateien aus einem Ordner in ein eigenes Sheet importieren. Du kannst ganz einfach den Ordner auswählen. Es gibt auch eine Browse funktion mit der du die einzelne Datei anwählen kannst, diese ist aber nicht eingebaut da ich ganze Ordner voll csv-Dateien verarbeite. Importiere deine Datei in ein separates sheet, dann führst du alle nötigen Operationen durch (löschen der ersten Zeilen ect.) und danach kopierst du die Informationen in dein Verarbeitungs Sheet und löschst das andere. Alles klar?
Public Sub Csv_import()
Debug.Print "importing .csv data... "
' declaration
Dim Anf As Long ' Anf for Anfang
Dim Appshell As Variant ' Appshell to browse folder
Dim ap As String ' ap for appostroph
Dim BrowseDir As Variant ' directory for selection
Dim f As Variant ' f for file
Dim csvPFAD As String
Dim wbTarget As Workbook, wbSource As Workbook, ws As Worksheet
Dim FSO As Object
Set FSO = CreateObject("Scripting.Filesystemobject")
Set wbTarget = ActiveWorkbook
Dim strTest As String
Dim Arr(3) As String
Dim Zähler As Long
Dim Zeile As String
Dim t() As String
Dim i As Long, k As Long
Dim c As Long
' initialization
ap = """"
Anf = 0
Zähler = 0
' get the storage folder
Set Appshell = CreateObject("Shell.Application")
Set BrowseDir = Appshell.BrowseForFolder(0, "Ordner auswählen", &H1000, "BITTE ORDNER PFAD HIER EINFÜGEN")
On Error GoTo Abbrechen
csvPFAD = BrowseDir.items().Item().path
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set wbSource = ActiveWorkbook
For Each f In FSO.GetFolder(csvPFAD).Files
If LCase(Right(f.name, 3)) = "csv" Then
Set ws = wbTarget.Worksheets.Add
ws.Move Before:=Worksheets("DEIN AUSWERTUNGSSHEET")
' name the sheet
On Error GoTo Fehler
'write the data from the csv file into the worksheet
Open csvPFAD & "\" & f.name For Input As 1
i = 1
' import all files into excel
Do Until EOF(1)
Line Input #1, Zeile
t = Split(Zeile, ";")
For k = 0 To UBound(t)
t(k) = Replace(t(k), ",", ".")
Cells(i, Anf + k + 1).Value = t(k)
Next k
i = i + 1
Loop
Close 1
End If
Application.ActiveSheet.name = "IMPORT" & Zähler + 1
Zähler = Zähler + 1
Next
' HIER BEARBEITEN UND KOPIEREN DEINER DATEN
Application.DisplayAlerts = False
c = 1
Do While wsexist("IMPORT" & c)
Worksheets("IMPORT" & c).Delete
c = c + 1
Loop
Set FSO = Nothing
Exit Sub
Abbrechen:
End
Fehler:
MsgBox (Err.Description)
End Sub
Public Function wsexist(wsName As String) As Boolean
Dim i As Integer
wsexist = False
For i = 1 To ActiveWorkbook.Sheets.count
If ActiveWorkbook.Sheets(i).name = wsName Then
wsexist = True
End If
Next
End Function
wsexist ist eine Funktion die feststellt ob dieses Worksheet existiert. Auch sonst nützlich würd ich mir irgendwo speichern wo du immer Zugriff hast z.B Persönliche Makromappe.
Flies away!
|