Solved

Excel VBA: Load csv file into multi dimensional array

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

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 48

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel 2013 Problem 12 48
Excel : show empty cell if zero 7 22
+1 to an even value 4 15
TT Auto DashBoard 4 33
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

744 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

15 Experts available now in Live!

Get 1:1 Help Now