+ Reply to Thread
Results 1 to 16 of 16

Reading values from a text file

Hybrid View

  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Reading values from a text file

    Hello Krish T,

    This macro will store the contents of two files in same directory into an Array(1 To 50, 1 To 2). You will need to add the file path and perhaps change the names of the two text files. We can expand this macro to do more later. Let me know how this works for you.
    Sub CopyFilesToArray()
    
      Dim Data As Variant
      Dim FileData() As Variant
      Dim FileArray As Variant
      Dim FileName As Variant
      Dim FilePath As String
      Dim I As Long, J As Long
      
        FilePath = "C:\"
        FileArray = Array("req_par1.txt", "req_par2.txt")
      
      
        ReDim FileData(1 To 50, 1 To UBound(FileArray) + 1)
        
          For Each FileName In FileArray
            J = J + 1
            FileName = FilePath & FileName
            Open FileName For Input As #1
              Do While Not EOF(1)
                I = I + 1
                Input #1, Data
                FileData(I, J) = Data
              Loop
            Close #1
            I = 0
          Next FileName
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  2. #2
    Forum Contributor
    Join Date
    10-01-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    125

    Re: Reading values from a text file

    Hi Leith Ross, Sorry for late response. Thanks for you reply and macro. I have modified this macro for my requirement but getting some problem.
     Sub CopyFilesToArray()
    
      Dim Data As Variant
      Dim FileData() As Variant
      Dim FileArray As Variant
      Dim FileName As Variant
      Dim FilePath As String
      Dim I As Long, J As Long
      Dim req_par(1 To 50) As String
      
        count = 2
        
        ' count is fixed for a particular case and calculated before this step
        
        FilePath = "C:\"
        
        ' FileArray = Array("req_par1.txt", "req_par2.txt")
        
        ' Trying to produce the above FileArray using below lines
        
        For r = 1 To count
        FileArray = Array("req_par" & CStr(r) & ".txt")   
        Next
        
        ' But not storing both text files. Only storing second one.
        ' Not producing the same result as given by you
        
      
        ReDim FileData(1 To 50, 1 To UBound(FileArray) + 1)
        
          For Each FileName In FileArray
            J = J + 1
            FileName = FilePath & FileName
            Open FileName For Input As #1
              Do While Not EOF(1)
                I = I + 1
                Input #1, Data
                FileData(I, J) = Data
              Loop
            Close #1
            I = 0
          Next FileName
          
    End Sub
    If I am able to produce FileArray similar to given by you, then my problem will be 95% solved. Can you suggest me on this?

    Its really awesome response from you on this problem.

    Thanks
    Krish T

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Reading values from a text file

    Hello Krish T,

    I made the changes to load the FileArray.
    Sub CopyFilesToArray()
    
      Dim Data As Variant
      Dim FileData() As Variant
      Dim FileArray As Variant
      Dim FileName As Variant
      Dim FilePath As String
      Dim I As Long, J As Long
      Dim req_par(1 To 50) As String
      
        Count = 2
        
        ' count is fixed for a particular case and calculated before this step
        
        FilePath = "C:\"
        
        ' FileArray = Array("req_par1.txt", "req_par2.txt")
        
        ' Trying to produce the above FileArray using below lines
        
        ReDim FileArray(Count - 1)
        
        For r = 1 To Count
          FileArray(r) = "req_par" & CStr(r) & ".txt"
        Next
        
        ' But not storing both text files. Only storing second one.
        ' Not producing the same result as given by you
        
      
        ReDim FileData(1 To 50, 1 To UBound(FileArray) + 1)
        
          For Each FileName In FileArray
            J = J + 1
            FileName = FilePath & FileName
            Open FileName For Input As #1
              Do While Not EOF(1)
                I = I + 1
                Input #1, Data
                FileData(I, J) = Data
              Loop
            Close #1
            I = 0
          Next FileName
          
    End Sub

  4. #4
    Forum Contributor
    Join Date
    10-01-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    125

    Re: Reading values from a text file

    Hi Leith, Thanks for your quick response. Here the above code is giving the problem in the following loop:
         For r = 1 To count
          FileArray(r) = "req_par" & CStr(r) & ".txt"
        Next
    so I have changed the above code as below and it is working:
     Sub CopyFilesToArray()
    
      Dim Data As Variant
      Dim FileData() As Variant
      Dim FileArray As Variant
      Dim FileName As Variant
      Dim FilePath As String
      Dim I As Long, J As Long
      Dim req_par(1 To 50) As String
      
        count = 2
        
        ReDim FileArray(count - 1) 
        
        FilePath = "C:\"
    
        For r = 0 To count - 1
        FileArray(r) = "req_par" & CStr(r + 1) & ".txt"
        Next
        
        ReDim FileData(1 To 50, 1 To UBound(FileArray) + 1)
    
        For Each FileName In FileArray
            J = J + 1
            FileName = FilePath & FileName
            Open FileName For Input As #1
              Do While Not EOF(1)
                I = I + 1
                Input #1, Data
                FileData(I, J) = Data
              Loop
            Close #1
            I = 0
          Next FileName
          
    End Sub
    Thanks
    Krish T

  5. #5
    Forum Contributor
    Join Date
    10-01-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    125

    Re: Reading values from a text file

    You are genius Leith. Solved my problem. Thanks a lot. Really helpful to me.

    Thanks
    Krish T

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Reading values from a text file

    Hello Krish T,

    Sorry about the oversight on the loop indices. I thought I had changed them. Glad to know it is working.

  7. #7
    Forum Contributor
    Join Date
    10-01-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    125

    Re: Reading values from a text file

    No problem Leith. Once again Thanks to you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1