Solved

Excel VBA: Load csv file into multi dimensional array

Posted on 2014-10-30
6
2,678 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 50

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 50

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 50

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

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!

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

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