PDA

Click to See Complete Forum and Search --> : Read text files?Pls Help


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.

RobDog888
Apr 22nd, 2003, 12:46 AM
Since you "Line Input #1, WholeLine" you can split the string on
the "," char. Then Trim on each element. This will remove the
spaces. The Split function returns an array of elements without
the ","s and you can loop through each array element to the
UBound entering the value into each Cell.

ITboy
Apr 22nd, 2003, 01:12 AM
i CAN'T use the Split function coz I using Excel 97.. Can u pls show me the code or modify my above code to do it. Thank u very much.

ITboy
Apr 22nd, 2003, 09:57 PM
I need to check the 4th line in a text file named parameters.txt. It will b either 0,1,2,3,4,5,6,7 or 8.I have 6 columns of data in Excel.Column A, B,C,D,E,F. I want to check thru the 4th and 5th columns(D,E). If any of the values is more than the num in the text file, I want the WHOLE ROW of data to be deleted and the next row of data to be shifted up in the Sheet.Hope u understand what I mean.Can anyone pls help.? Its urgent.I dunno how to do it.Thanks