Solved

Excel VBA: Load csv file into multi dimensional array

Posted on 2014-10-30
6
2,371 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
  • 3
  • 3
6 Comments
 
LVL 49

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 49

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now