Option Public
Option Declare
Sub Initialize
On Error Goto errhandler
Dim filename As String
Dim filename2 As String
Dim filenum As Integer
Dim filenum2 As Integer
Dim txt As String
Dim txt2 As string
Dim zwerg As String
Dim pos1 As Integer
Dim pos2 As Integer
Dim anzDel As Integer
Dim maxDel As Integer
Dim a As integer
Dim rowCount As Integer
fileName = "Y:\input.csv"
fileName2 = "Y:\output.csv"
filenum = FreeFile()
Open fileName For Input As filenum
filenum2 = FreeFile()
Open fileName2 For Output As filenum2
Do While Not EOF(fileNum)
Line Input #fileNum, txt
If rowCount = 0 Then
'// Titlelzeile extrahieren um die Anzahl der Einnträge zu bestimmen
Dim titleArray As Variant
titleArray = C_Explode(txt, ";")
maxDel = UBound(titleArray)
rowCount = 1
End if
nochmal:
anzDel = C_Get_SemiNo(txt)
If anzDel <> maxDel then
For a = 1 To Len(txt)
If Mid$(txt, a, 1) = ";" Then
anzDel = anzDel + 1
End If
'Ist es evtl. ein Kommentarfeld?
If Mid$(txt, a, 2) = {;"} Then
pos1 = InStr(txt, {;"})
pos2 = InStr(pos1, txt, {";})
'Kommentar
If pos2 > pos1 Then
'Der abschließende Delimiter für den Komentar befindet sich in der gleichen Zeile.
'Sonderzeichen im Kommentar entfernen
zwerg = Mid$(txt, pos1+1, pos2-pos1)
zwerg = C_ReplaceSubstring(zwerg, {"}, "")
zwerg = C_ReplaceSubstring(zwerg, {;}, " ")
txt = Left$(txt, pos1) + zwerg + Mid$(txt, pos2+1, Len(txt)-pos2)
If InStr(txt, {;"}) > 0 Then
GoTo nochmal
Else
If C_Get_SemiNo(txt) <> maxDel Then
Line Input #fileNum, txt2
txt = txt + " " +txt2
GoTo nochmal
Else
Exit For
End If
End if
Else
'Kein End-Kommentarzeichen in der aktuellen Zeile gefunden --> nächste Zeile einlesen
Line Input #fileNum, txt2
txt = txt +" " + txt2
GoTo nochmal
End If
End If
Next
End if
print #fileNum2, txt
Loop
Close filenum
Close filenum2
Exit Sub
errhandler:
MsgBox "Fehler: " & Error & " (" & Err & ") in Zeile: " & Erl , 64, "Hinweis!"
Exit Sub
End Sub
Function C_Get_SemiNo(txt) As Integer
On Error GoTo errhandler
Dim a As Integer
Dim lauf As Integer
For a = 1 To Len(txt)
If Mid$(txt, a, 1) = ";" Then
If Mid$(txt, a, 2) = {;"} Then
Exit Function
End if
lauf = lauf + 1
End If
Next
C_Get_SemiNo = lauf
Exit Function
errhandler:
MsgBox "Fehler in C_Get_SemiNo() Error: " & Error & " (" & Err & ") in Zeile: " & Erl , 64, "Hinweis!"
Exit Function
End Function