T
theSquirrel
I am currently working on a project that requires me to dynamically add
items to a listbox.
HAHA, if it were just that easy!
The listbox I am trying to populate is a 14 column monster named
"lstMain". I am pulling information from an Excel database.
I am having problems while sending adding the items in a loop. I can
add a line individually but once I introduce the loop it fails with a
type mismatch.
Here is the code and issue details.
The program progression is as follows:
- UI initializes
- UI Activates, where it dynamically populates a listbox called
"lstSeriesName"
- User selects an item from lstSeriesName to display items in lstMain
- get an error before displaying anything.
The failing code:
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
Private Sub lstSeriesName_Change()
'when clicked, list the relevent cards
'If SelectingAll = True Then Exit Sub
'If ClearingAll = True Then Exit Sub
'clear the card list to make way for the new list
Me.lstMain.Clear
'create a loop to loop through all the cells in the list
Dim StartRow As Long
Dim curCell As Range
Dim RowIndex As Long
StartRow = 20
CardsShowing = 0
For RowIndex = StartRow To LastRow
Set curCell = Worksheets(DBName).Cells(RowIndex, 1)
If Me.lstSeriesName.Value = "All" Then
If FilterFunction(DBName, RowIndex) = True Then
Me.lstMain.AddItem RowArray(RowIndex)
CardsShowing = CardsShowing + 1
End If
ElseIf curCell.Value = Me.lstSeriesName.Value And
FilterFunction(DBName, RowIndex) = True Then
CardsShowing = CardsShowing + 1
Me.lstMain.AddItem RowArray(RowIndex)
End If
Next RowIndex
Me.lblCardsShowing.Caption = CardsShowing
Me.lblTotalCards.Caption = TotalCards
End Sub
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
Notes about above code:
1.) LastRow is a form level variable that holds the rowindex of the
lastrow of data.
2.) DBName is a form level variable that holds the name of the database
we are pulling information from
3.) FilterFunction is a UDF that functions properly in another part of
the project. (i can post that if its relevent)
Supporting Code:
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
Public Function RowArray(RowIndex As Long) As Variant
Dim MyArray(13) As Variant
With Worksheets("CardDatabase")
MyArray(0) = .Cells(RowIndex, 2).Value
MyArray(1) = .Cells(RowIndex, 3).Value
MyArray(2) = .Cells(RowIndex, 4).Value
MyArray(3) = .Cells(RowIndex, 5).Value
MyArray(4) = .Cells(RowIndex, 6).Value
MyArray(5) = .Cells(RowIndex, 10).Value
MyArray(6) = .Cells(RowIndex, 12).Value
MyArray(7) = .Cells(RowIndex, 13).Value
MyArray(8) = .Cells(RowIndex, 14).Value
MyArray(9) = .Cells(RowIndex, 15).Value
MyArray(10) = .Cells(RowIndex, 16).Value
MyArray(11) = .Cells(RowIndex, 17).Value
MyArray(12) = .Cells(RowIndex, 18).Value
MyArray(13) = .Cells(RowIndex, 19).Value
End With
RowArray = MyArray
Erase MyArray
End Function
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
The problem comes from the following line of code from teh first
section of code:
Me.lstMain.AddItem RowArray(RowIndex)
With this line I get a Type Mismatch error.
I have also tried the following variations:
Me.lstMain.Column RowArray(RowIndex)
Me.lstMain.List RowArray(RowIndex)
and got an Invalid use of property error.
I am at an end of my ideas on how to get this to function correctly.
Any help would be great, also if any additional code is needed, please
ask and I will post it.
Thanks in advance,
theSquirrel
items to a listbox.
HAHA, if it were just that easy!
The listbox I am trying to populate is a 14 column monster named
"lstMain". I am pulling information from an Excel database.
I am having problems while sending adding the items in a loop. I can
add a line individually but once I introduce the loop it fails with a
type mismatch.
Here is the code and issue details.
The program progression is as follows:
- UI initializes
- UI Activates, where it dynamically populates a listbox called
"lstSeriesName"
- User selects an item from lstSeriesName to display items in lstMain
- get an error before displaying anything.
The failing code:
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
Private Sub lstSeriesName_Change()
'when clicked, list the relevent cards
'If SelectingAll = True Then Exit Sub
'If ClearingAll = True Then Exit Sub
'clear the card list to make way for the new list
Me.lstMain.Clear
'create a loop to loop through all the cells in the list
Dim StartRow As Long
Dim curCell As Range
Dim RowIndex As Long
StartRow = 20
CardsShowing = 0
For RowIndex = StartRow To LastRow
Set curCell = Worksheets(DBName).Cells(RowIndex, 1)
If Me.lstSeriesName.Value = "All" Then
If FilterFunction(DBName, RowIndex) = True Then
Me.lstMain.AddItem RowArray(RowIndex)
CardsShowing = CardsShowing + 1
End If
ElseIf curCell.Value = Me.lstSeriesName.Value And
FilterFunction(DBName, RowIndex) = True Then
CardsShowing = CardsShowing + 1
Me.lstMain.AddItem RowArray(RowIndex)
End If
Next RowIndex
Me.lblCardsShowing.Caption = CardsShowing
Me.lblTotalCards.Caption = TotalCards
End Sub
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
Notes about above code:
1.) LastRow is a form level variable that holds the rowindex of the
lastrow of data.
2.) DBName is a form level variable that holds the name of the database
we are pulling information from
3.) FilterFunction is a UDF that functions properly in another part of
the project. (i can post that if its relevent)
Supporting Code:
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
Public Function RowArray(RowIndex As Long) As Variant
Dim MyArray(13) As Variant
With Worksheets("CardDatabase")
MyArray(0) = .Cells(RowIndex, 2).Value
MyArray(1) = .Cells(RowIndex, 3).Value
MyArray(2) = .Cells(RowIndex, 4).Value
MyArray(3) = .Cells(RowIndex, 5).Value
MyArray(4) = .Cells(RowIndex, 6).Value
MyArray(5) = .Cells(RowIndex, 10).Value
MyArray(6) = .Cells(RowIndex, 12).Value
MyArray(7) = .Cells(RowIndex, 13).Value
MyArray(8) = .Cells(RowIndex, 14).Value
MyArray(9) = .Cells(RowIndex, 15).Value
MyArray(10) = .Cells(RowIndex, 16).Value
MyArray(11) = .Cells(RowIndex, 17).Value
MyArray(12) = .Cells(RowIndex, 18).Value
MyArray(13) = .Cells(RowIndex, 19).Value
End With
RowArray = MyArray
Erase MyArray
End Function
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
The problem comes from the following line of code from teh first
section of code:
Me.lstMain.AddItem RowArray(RowIndex)
With this line I get a Type Mismatch error.
I have also tried the following variations:
Me.lstMain.Column RowArray(RowIndex)
Me.lstMain.List RowArray(RowIndex)
and got an Invalid use of property error.
I am at an end of my ideas on how to get this to function correctly.
Any help would be great, also if any additional code is needed, please
ask and I will post it.
Thanks in advance,
theSquirrel