I will preface this with the fact I am not very proficient with Excel. I use Access occassionally.
Now, is there a way I can populate a list box from a spreadsheet, but use every sixth line, i.e. A1, A6, A12 until there are no more entries? Here's what I have:
I am importing a text file. The names and addresses are not split up but each 6th line starts a new record. I only need the name in the list box so the user can make choose from a list of customers.
Any thoughts would be greatly appreciated. Here's the code I am using as an example. Thanks in advance.
Sub PopulateListWithUniqueItems()
Dim anyR As Range
Dim listCollection As New Collection
Dim cMember
Dim I As Integer
Dim J As Integer
Dim lCount As Integer
Dim cell As Range
Dim tempS
Dim myList()
'in this example, the entries are assumed to be in
'cells A1 to A20.
Set anyR = Range("=Customers!A1:A20"
'on error must be set as an error is created
'when a duplicate item is added to the listCollection
'and we want the macro to continue
On Error Resume Next
For Each cell In anyR
If Not IsEmpty(cell) Then
'second argument as shown is needed
listCollection.Add cell.Value, CStr(cell.Value)
End If
Next
'turn off error handling
On Error GoTo 0
'assign to a list for easier use
lCount = listCollection.Count
ReDim myList(1 To lCount)
For Each cMember In listCollection
I = I + 1
myList(I) = cMember
Next
'sort the list
For I = 1 To lCount - 1
For J = I + 1 To lCount
If myList(I) > myList(J) Then
tempS = myList(I)
myList(I) = myList(J)
myList(J) = tempS
End If
Next
Next
'assign to listbox
UserForm1.ListBox1.List = myList
'display form
UserForm1.Show
End Sub
Now, is there a way I can populate a list box from a spreadsheet, but use every sixth line, i.e. A1, A6, A12 until there are no more entries? Here's what I have:
I am importing a text file. The names and addresses are not split up but each 6th line starts a new record. I only need the name in the list box so the user can make choose from a list of customers.
Any thoughts would be greatly appreciated. Here's the code I am using as an example. Thanks in advance.
Sub PopulateListWithUniqueItems()
Dim anyR As Range
Dim listCollection As New Collection
Dim cMember
Dim I As Integer
Dim J As Integer
Dim lCount As Integer
Dim cell As Range
Dim tempS
Dim myList()
'in this example, the entries are assumed to be in
'cells A1 to A20.
Set anyR = Range("=Customers!A1:A20"
'on error must be set as an error is created
'when a duplicate item is added to the listCollection
'and we want the macro to continue
On Error Resume Next
For Each cell In anyR
If Not IsEmpty(cell) Then
'second argument as shown is needed
listCollection.Add cell.Value, CStr(cell.Value)
End If
Next
'turn off error handling
On Error GoTo 0
'assign to a list for easier use
lCount = listCollection.Count
ReDim myList(1 To lCount)
For Each cMember In listCollection
I = I + 1
myList(I) = cMember
Next
'sort the list
For I = 1 To lCount - 1
For J = I + 1 To lCount
If myList(I) > myList(J) Then
tempS = myList(I)
myList(I) = myList(J)
myList(J) = tempS
End If
Next
Next
'assign to listbox
UserForm1.ListBox1.List = myList
'display form
UserForm1.Show
End Sub