AW: Einige Stellen aus TXT-File auslesen
Josef
Hallo André,
das ist kein Problem.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************
Option Explicit
Sub importText()
Dim strFile As String, varText As Variant, varInput As Variant
Dim lngRow As Long, lngIndex As Long, intFF As Integer
Dim strTmp As String, intPos As Integer
strFile = Application.GetOpenFilename("Textdateien (*.txt),*.txt")
If strFile = CStr(False) Then Exit Sub
varText = Range("A1:F1")
Range("A2:F" & Rows.Count) = ""
Range("A2:F" & Rows.Count).NumberFormat = "General"
varInput = Range("A2:F" & Rows.Count)
intFF = FreeFile
Open strFile For Input As #intFF
Do While Not EOF(intFF)
Line Input #intFF, strTmp
For lngIndex = 1 To UBound(varText, 2)
intPos = InStr(Application.Max(1, intPos), strTmp, varText(1, lngIndex))
If intPos > 0 Then
If lngIndex = 1 Then lngRow = lngRow + 1
If varInput(lngRow, lngIndex) = "" Then
varInput(lngRow, lngIndex) = Trim(Mid(strTmp, intPos + Len(varText(1, lngIndex)) + 1, IIf(lngIndex < 4, 30, 14)))
If IsNumeric(varInput(lngRow, lngIndex)) Then
varInput(lngRow, lngIndex) = varInput(lngRow, lngIndex) * 1
ElseIf IsDate(varInput(lngRow, lngIndex)) Then
varInput(lngRow, lngIndex) = CDate(varInput(lngRow, lngIndex))
Else
varInput(lngRow, lngIndex) = ReplaceMulti(varInput(lngRow, lngIndex), " ", " ")
End If
End If
End If
Next
Loop
Close #intFF
Range("A2:F" & Rows.Count) = varInput
Range("A1:F1").EntireColumn.AutoFit
End Sub
Private Function ReplaceMulti(ByVal Expression As String, ByVal Find As String, ByVal Repl As String) As String
ReplaceMulti = Expression
Do While InStr(1, ReplaceMulti, Find) > 0
ReplaceMulti = Replace(ReplaceMulti, Find, Repl)
Loop
End Function
Gruß Sepp