ITboy
Apr 22nd, 2003, 12:21 AM
How am I to read in a text file in xl if I have spaces in them(see attached gantry_4.txt). I know how to read in them without any spaces(see attached gantry_3.txt). My codes for reading gantry_3.txt is as follows:
Private Sub GetTT()
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim File As Scripting.File
Dim Sep As String
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Dim file2 As String, file3 As String
Sep = ","
'go to MAPVIEW sheet
Worksheets("Sheet1").Activate
Set fso = New Scripting.FileSystemObject
Set fld = fso.GetFolder("C:\GetTT")
'cycle to each file in the folder
For Each File In fld.Files
'this returns the whole path name of the file
Debug.Print File.Name
'Add the codes to open or do what you need in this text file
Application.ScreenUpdating = False
'On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open File.Path For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
Next
End Sub
How am I to set a condition to check if my text file were to be like gantry_4.txt, it should read it in the same format? Thanks for the help.
Private Sub GetTT()
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim File As Scripting.File
Dim Sep As String
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Dim file2 As String, file3 As String
Sep = ","
'go to MAPVIEW sheet
Worksheets("Sheet1").Activate
Set fso = New Scripting.FileSystemObject
Set fld = fso.GetFolder("C:\GetTT")
'cycle to each file in the folder
For Each File In fld.Files
'this returns the whole path name of the file
Debug.Print File.Name
'Add the codes to open or do what you need in this text file
Application.ScreenUpdating = False
'On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open File.Path For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
Next
End Sub
How am I to set a condition to check if my text file were to be like gantry_4.txt, it should read it in the same format? Thanks for the help.