Hi, I'm new to the forum and I'm looking for some help. I have a decent background in other programming languages but am new to VBA. I've done alright so far, but now I'm stuck.
I need help splitting up a string that is passed to a function. This string comes from a listbox. Then, the split up parts must be put into an array (or something) and then those individual strings need to be searched for on the worksheet. When the cell is found, it needs to be added to a range that gets returned from the function.
Here is my code so far:
Private Sub EnterButton_Click()
' Call header selecting function
myHeadings = SelectHeadings
Unload Me
' Call range finder function
HeaderRange = FindHeader(myHeadings)
End Sub
Function SelectHeadings() As String
Dim lItem As Long
Dim headings As String
For lItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lItem) = True Then
headings = headings & ListBox1.List(lItem) & ","
ListBox1.Selected(lItem) = False
End If
Next
headings = Left(headings, Len(headings) - 1)
SelectHeadings = headings
MsgBox ("Your selections have been saved!")
End Function
Function FindHeader(headerStr) As Range
' Split up header string
Dim s() As String
Dim i As Integer
s = Split(headerStr, ",")
For i = 0 To UBound(s)
s(i) = Trim(s(i)) & ","
Next
' Loop through string array finding corresponding cells
Dim foundRange() As Range
With Worksheets(1)
For i = 0 To UBound(s)
Set c = .Find(s(i), LookIn:=xlValues)
foundRange(i) = c
Next i
End With
FindHeader = foundRange
End Function