Solved

Excel VBA: Load csv file into multi dimensional array

Posted on 2014-10-30
6
2,751 Views
Last Modified: 2014-10-30
Hello,

I need to load a comma separated csv file into a multidimensional array in excel vba, and I'm wondering if there's a better way than looping through it which I of course could do.

ExampleFile.csv:

"Header1","header2","header3","header4","header5"[crlf]
"row1col1data","row1col2data","row1col3data","row1col4data","row1col5data"[crlf]
"row2col1data","row2col2data","row2col3data","row2col4data","row2col5data"[crlf]

I've defined an array:

Dim GTArr(45, 1) As String ' (there are 45 columns)

Question #1, how do I easiest add the file into the array?  and #2, should I read the number of lines in the text file first to get the numbers of rows in the array or redim it for each row I add?

Redim Preserve GTArr(45, CurrentRow+1)

thanks
~j
0
Comment
Question by:prosit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40413137
Hi,

you could open it with excel

and then assign the used range to an array (previously defined as variant)

Sub Macro()
Dim aMyArray As Variant
Set myRng = ActiveSheet.UsedRange
aMyArray = myRng
End Sub

Regards
0
 
LVL 2

Author Comment

by:prosit
ID: 40413145
Well.. I guess I should have been more precise in what I needed.  The text will eventually go into a spreadsheet, in a certain column, depending on the column of the text file, that's why I need it in an array first.
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40413211
What I mean is you open you csv load in your array and then close your csv

you can the manipulate your array before paste it in excel
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:prosit
ID: 40413305
I don't think I understand.  

I don't want to open the file in excel first; it's an automated process, read file, parse it into the columns I want then save spreadsheet and we're done.

~j
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40413388
Hi,

pls try

Sub Macro()

strCsv = "aa,bb,cc dd" & vbCrLf & "a,b,c,d" & vbCrLf & "a,b,c,d"
Dim aMyArray As Variant
Dim aMyArrayFinal As Variant
aMyArray = Split(strCsv, vbCrLf)
FstDimension = UBound(aMyArray)
SndDimension = UBound(Split(aMyArray(0), ","))
ReDim myArrayFinal(FstDimension, SndDimension)
For Idx1 = LBound(aMyArray) To UBound(aMyArray)
    For Idx2 = 0 To SndDimension
        myArrayFinal(Idx1, Idx2) = Split(aMyArray(Idx1), ",")(Idx2)
    Next
Next
End Sub

Open in new window

0
 
LVL 2

Author Closing Comment

by:prosit
ID: 40414216
It wasn't what I was looking for but I resolved it myself...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question