Results 1 to 4 of 4

Thread: Read text files?Pls Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86

    Question Read text files?Pls Help

    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:

    PHP Code:
    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 Stringfile3 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.
    Attached Files Attached Files

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86
    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86

    Question Check columns in Excel ?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width