Excel VBA - Working with arrays


Hello,
I have used the code below to open a file, read the first line of a file into an array called headings(1)
The headings wil be in the format first name, last name, stud num, test1, test 2, test3, (Right up to test n),testn.  Each heading is separated by a comma in the array.
How would I go about extracting each heading into a separate variable or array, bearing in mind that there could be five headings or 150? I know that I need to look for each comma and when it finds the comma store each heading separately.

Sub ReadFirstLineFile()
Dim Headings(1) As String

Open "A:\Implementation\inputfile.csv" For Input As #1
Line Input #1, Headings(1)
MsgBox "First Line " & "in input file reads:" & Chr(13) & Headings(1)
Close #1

End Sub

The reason I need to do this is that the user needs to choose which headings to include in the analysis, so i need to extract each heading name and present it to the user to choose yes or no.  Once I separate each heading name, would it be possible to pass this to a user form which could present it as a list of choices ?

Many thanks in advance

Regards,
natalie vance


NatalieVanceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

spiritwithinCommented:
Hi Natalie!

Well i think what you could do is:

Add a ListBox control to your form. Set the "Style" property of the ListBox to "1" (checkboxes).

' Assuming you have a form called frmMy and
' a listbox called lsbMy, and that your CSV-File
' values are limited by a semicolon: ";"

Sub ReadFirstLineFile()

 Dim strHeadings, strContent() As String
 Dim iCtr as Integer

 Open "A:\Implementation\inputfile.csv" For Input As #1
  Line Input #1, strHeadings
 Close #1

 strContent = Split(strHeadings, ";")
 For iCtr = 0 to ubound(strContent)
  frmMy.lsbMy.AddItem strContent(iCtr)
 Next

End Sub


I did not test it, so i do not know if it works. But it should. If you have further questions, feel free to ask.
0
spiritwithinCommented:
Btw.: please note that the function will produce a runtime error if the heading string does not contain what you assume.

In other words:

If strContent is not a correctly filled array after calling Split(), then UBound(strContent) will produce a runtime error.

If you want to avoid this go like that:

On Error Resume Next
For iCtr = 0 to ubound(strContent)
 frmMy.lsbMy.AddItem strContent(iCtr)
Next
On Error Goto 0

This disables runtime error checking for the code between the two statements.

Cheers.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NatalieVanceAuthor Commented:
Many thanks for your help and suggestions.

Regards,
natalie
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.