Solved

Excel VBA: Load csv file into multi dimensional array

Posted on 2014-10-30
6
2,859 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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…

696 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