+ Reply to Thread
Results 1 to 16 of 16

Reading values from a text file

Hybrid View

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

    Reading values from a text file

    I have two text files attached below and having some parameters in those. I would like to store these parameters from each text file in an array in VBA.

    Can any body help me on this?

    Thanks in advance.
    Attached Files Attached Files
    Last edited by krish T; 10-21-2010 at 11:56 AM. Reason: Solved the problem

  2. #2
    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,

    What will the array dimensions be?
    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!)

  3. #3
    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,

    Array will be like as below:
     Dim Table(1 To 50, 1 To 50) As String
    . First it has to open first text file, read the first line and save as Table(1,1) and second line as Table(2,1) and 3rd line as Table(3,1) and 4th line as Table (4,1). Next it has to open second text file and save the 4 lines as Table(1,2), Table(2,2), Table(3,2) and Table(4,2).

  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

    Hello Leith Ross,

    Can you answer me for the above problem? Appreciate your help.

  5. #5
    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,

    Based on your previous post, I believe the array dimensions should be Array(1 To 50, 1 to 2), yes?

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

    Re: Reading values from a text file

    That's correct Leith Ross based on above example. But actually the second dimension in Array(1 To 50, 1 to 2) may vary like Array(1 To 50, 1 to 3) or Array(1 To 50, 1 to 10). If you can guide me based on above example, I will try to change it to my required dimension in the array. Thanks for your reply.

    Krish T

  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

    Any help will appreciate on above problem.

    Thanks in advance.

  8. #8
    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 have been having problems with my computer today. What will determine the size of the second dimension in the array, i.e. Array(1 To 50, 1 To 10), Array(1 To 50, 1 to 20)?

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

    Re: Reading values from a text file

    Hello Leith Ross,

    I will give brief description about my problem. I have some parameters in the xls sheet which will be given by me or an user like p1,p2...etc. So I have written the code to count these parameters. So my second dimension in the array is this count and I will be having text files in a folder equal to this count. This count is fixed for a particular case. Please assume my count is 2 for time being means second dimension in the array is Array(1 To 50, 1 To 2) i.e, I am having two text files in a folder. Please suggest me based on this example.

    Please let me know if you need any other information.

    Thanks
    Krish T

  10. #10
    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

  11. #11
    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

  12. #12
    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

  13. #13
    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

+ 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