Gruppe
Extern
Bereich
TextImport
Thema
VBA-Import einer Textdatei mit Semikoli als Feldtrenner
Problem
Die in Zelle P1 genannte Textdatei soll importiert werden. Es handelt sich um eine Texttdatei mit Semikoli als Feldtrenner.
Lösung
Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.
StandardModule: Modul1
Sub TextImport()
Dim iRow As Integer, iCol As Integer
Dim sFile As String, sTxt As String
sFile = Range("P1").Value
If Dir(sFile) = "" Then
Beep
MsgBox "Datei wurde nicht gefunden!"
Exit Sub
End If
iRow = 1
iCol = 1
Close
Open sFile For Input As #1
Do Until EOF(1)
Line Input #1, sTxt
Do While InStr(sTxt, ";")
Cells(iRow, iCol).Value = Left(sTxt, InStr(sTxt, ";") - 1)
sTxt = Right(sTxt, Len(sTxt) - InStr(sTxt, ";"))
iCol = iCol + 1
Loop
Cells(iRow, iCol).Value = sTxt
iRow = iRow + 1
iCol = 1
Loop
Close
End Sub