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.
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.
Last edited by krish T; 10-21-2010 at 11:56 AM. Reason: Solved the problem
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Hi Leith Ross,
Array will be like as below:. 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).![]()
Dim Table(1 To 50, 1 To 50) As String
Hello Leith Ross,
Can you answer me for the above problem? Appreciate your help.
Hello krish T,
Based on your previous post, I believe the array dimensions should be Array(1 To 50, 1 to 2), yes?
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
Any help will appreciate on above problem.
Thanks in advance.
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)?
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
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
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.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?![]()
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
Its really awesome response from you on this problem.
Thanks
Krish T
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
Hi Leith, Thanks for your quick response. Here the above code is giving the problem in the following loop:so I have changed the above code as below and it is working:![]()
For r = 1 To count FileArray(r) = "req_par" & CStr(r) & ".txt" Next
Thanks![]()
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
Krish T
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks