Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

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


0
NatalieVance
Asked:
NatalieVance
  • 2
1 Solution
 
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
 
NatalieVanceAuthor Commented:
Many thanks for your help and suggestions.

Regards,
natalie
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now