Results 1 to 2 of 2

Thread: input from a specific column and row in a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    1

    input from a specific column and row in a text file

    hi,

    I am trying to read and input data form a text file made of several columns and rows. I read somethign about "my reader" but I couldn't find the exact information.

    Thanks
    Hala

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: input from a specific column and row in a text file

    The following code might help you:
    VB Code:
    1. ' How to Read Line By Line & Retrieve Each Word From Text File
    2.  
    3. Option Explicit
    4.  
    5. Private Sub Form_Load()
    6. ' An example of using the ImportDelimitedFile routine
    7.     Dim values() As Variant, i As Long, j As Long
    8.     values() = ImportDelimitedFile("c:\temp\sri.txt", " ")
    9.     ' Values(0)(n) is the name of the Nth field.
    10.     ' Values(i)(n) is the value of the Nth field on the ith record.
    11.     ' For example, see how you can increment employees' salaries by 20%.
    12.     For i = 0 To UBound(values)
    13.         For j = 0 To UBound(values(i))
    14.             Debug.Print values(i)(j)
    15.         Next
    16.     Next
    17. End Sub
    18.  
    19. Function ReadTextFileContents(filename As String) As String
    20.     Dim fnum As Integer, isOpen As Boolean
    21.     On Error GoTo Error_Handler
    22.     ' Get the next free file number.
    23.     fnum = FreeFile()
    24.     Open filename For Input As #fnum
    25.     ' If execution flow got here, the file has been open without error.
    26.     isOpen = True
    27.     ' Read the entire contents in one single operation.
    28.     ReadTextFileContents = Input(LOF(fnum), fnum)
    29.     ' Intentionally flow into the error handler to close the file.
    30. Error_Handler:
    31.     ' Raise the error (if any), but first close the file.
    32.     If isOpen Then Close #fnum
    33.     If Err Then Err.Raise Err.Number, , Err.Description
    34. End Function
    35.  
    36. ' Load a text file into a TextBox control.
    37. 'Text1.Text = ReadTextFileContents("c:\bootlog.txt")
    38.  
    39. Function ImportDelimitedFile(filename As String, Optional delimiter As String = vbTab) As Variant()
    40.     Dim lines() As String, i As Long
    41.     ' Get all lines in the file.
    42.     lines() = Split(ReadTextFileContents(filename), vbCrLf)
    43.     ' To quickly delete all empty lines, load them with a special char.
    44.     For i = 0 To UBound(lines)
    45.         If Len(lines(i)) = 0 Then lines(i) = vbNullChar
    46.     Next
    47.     ' Then use the Filter function to delete these lines.
    48.     lines() = Filter(lines(), vbNullChar, False)
    49.     ' Create a string array out of each line of text
    50.     ' and store it in a Variant element.
    51.     ReDim values(0 To UBound(lines)) As Variant
    52.     For i = 0 To UBound(lines)
    53.         values(i) = Split(lines(i), delimiter)
    54.     Next
    55.     ImportDelimitedFile = values()
    56. End Function
    CS

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