Link to home
Start Free TrialLog in
Avatar of NatalieVance
NatalieVance

asked on

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


Avatar of spiritwithin
spiritwithin

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.
ASKER CERTIFIED SOLUTION
Avatar of spiritwithin
spiritwithin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NatalieVance

ASKER

Many thanks for your help and suggestions.

Regards,
natalie