Class Array to Userform Listbox

james87

New Member
Joined
Mar 4, 2011
Messages
8
Hi Guys,

I'm stuck! I've got a class of bidders (called mobjbidder) ... and in there I have their name, starting price, target price and bid increment (each are called mobjbidder.name , mobjbidder.startingprice ... etc)

These have all been loaded into the array.

I have a listbox in a userform with 4 columns. I want each bidders name and info on each row, and each column to contain just the name, starting price etc.

I've tried:

Code:
For i = 0 To UBound(mobjBidder())
frmAuctionProject.ListBoxBids.AddItem mobjBidder(i).Name
Next i

But this doesnt work ....

Any help would be much appreciated!!!

Many Thanks

James:confused:
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
hey, thank you for the quick response!

is this what u need?

Code:
Option Explicit

Private Const mlngENTRIESPERROW As Long = 4&

Private mstrName As String
Private mdblVcoord As Double
Private mdblScoord As Double
Private mdblZcoord As Double
Private mdblPCurrent As Double

Public Property Get Name() As String
    Name = mstrName
End Property

Public Property Get v() As Double
    v = mdblVcoord
End Property

Public Property Get s() As Double
    s = mdblScoord
End Property

Public Property Get z() As Double
    z = mdblZcoord
End Property

Public Property Get Current() As Double
    Current = mdblPCurrent
End Property


' Extracts the ID, xcoord and ycoord component data from the CSV file row, strRow.
' strRow should match the format required for a City row.
' Returns True if the row and the data conform to the required format, False otherwise.
Public Function ReadDataFromRow(ByVal strRow As String) As Boolean
    
    Dim strEntry As String
    
    Const gstrCOMMA As String = ","
    
    ReadDataFromRow = False
    
    If CountDelimitedEntries(strRow, gstrCOMMA) <> mlngENTRIESPERROW Then
        Exit Function
    End If
        
    mstrName = DetachNextDelimitedEntry(strRow, gstrCOMMA)
    If Len(mstrName) = 0 Then
        Exit Function
    End If

    strEntry = DetachNextDelimitedEntry(strRow, gstrCOMMA)
    If Not IsNumeric(strEntry) Then
        Exit Function
    End If
    mdblVcoord = CDbl(strEntry)
    
    strEntry = DetachNextDelimitedEntry(strRow, gstrCOMMA)
    If Not IsNumeric(strEntry) Then
        Exit Function
    End If
    mdblScoord = CDbl(strEntry)
    
    strEntry = DetachNextDelimitedEntry(strRow, gstrCOMMA)
    If Not IsNumeric(strEntry) Then
        Exit Function
    End If
    mdblZcoord = CDbl(strEntry)
    
    ReadDataFromRow = True
    
End Function
' Returns the number of delimiter-separated entries in strLine.
' Note that a final entry is presumed to exist after the final delimiter,
' so the result returned if effectively the number of delimiters, plus 1.
' The only exception is the empty string, for which a value of 0 is returned.
Public Function CountDelimitedEntries(ByVal strLine As String, ByVal strDelimiter As String) As Long

    Dim lngPos As Long
    Dim lngCount As Long
    
    CountDelimitedEntries = 0&
    
    If Len(strLine) = 0 Then
        Exit Function
    End If
    
    strLine = strLine & strDelimiter
    lngCount = 0
    lngPos = 0
    
    Do
        lngCount = lngCount + 1
        lngPos = InStr(lngPos + 1, strLine, strDelimiter, vbTextCompare)
    Loop While lngPos < Len(strLine)
    
    CountDelimitedEntries = lngCount

End Function

' Returns the substring from the start of strLine up to, but excluding, the
' first delimiter (or the whole string if no delimiters are found).
' Note that strLine is then stripped of the entry and the delimiter (if it exists)
Public Function DetachNextDelimitedEntry(ByRef strLine As String, ByVal strDelimiter As String) As String

    Dim lngPos As Long
    
    lngPos = InStr(strLine, strDelimiter)
    If lngPos > 0 Then
        DetachNextDelimitedEntry = Left$(strLine, lngPos - 1)
        strLine = Mid$(strLine, lngPos + 1)
    Else
        DetachNextDelimitedEntry = strLine
        strLine = ""
    End If
        
End Function

I've made slight progress by using:

Code:
frmAuctionProject.ListBoxBids.AddItem (mobjBidder(1).Name)

But i still dont know how to write each aspect of each bidder into a different column, and each bidder onto a different row.

Thanks again!
 
Upvote 0
You can assign an array to a listbox as its "list" - all at once. Something like (where a is an array):

Listbox1.List = a
 
Upvote 0
If that works so should:

Code:
For i = LBound(mobjBidder) To UBound(mobjBidder)
    frmAuctionProject.ListBoxBids.AddItem mobjBidder(i).Name
Next i

To populate the other columns use the List property, eg:

Code:
frmAuctionProject.ListBoxBids.List(i, 1) =  mobjBidder(i).StartingPrice

In a ListBox the rows and columns are zero based.
 
Upvote 0
thank u so much for your help,

so i can get a list of all the names of the bidders by doing as you said:

Code:
    For i = 1 To BidderCount
        frmAuctionProject.ListBoxBids.AddItem (mobjBidder(i).Name)
    Next i

but when i try :

Code:
    For i = 1 To BidderCount
        frmAuctionProject.ListBoxBids.List(i,1) (mobjBidder(i).Starting Price)
    Next i

i get the error :

Run Time Error '381' :
Could not set the List prloperty. Invalid property array index.

In the design mode of the usrform, I've set ColumnCount 4.

Thanks again.
 
Upvote 0
As I said the rows are zero based so try:

Rich (BB code):
For i = 1 To BidderCount
    frmAuctionProject.ListBoxBids.AddItem (mobjBidder(i).Name)
    frmAuctionProject.ListBoxBids.List(i - 1,1) (mobjBidder(i).Starting Price)
Next i
 
Upvote 0
ah that works perfectly. thank u!!

so another thing i've been looking for as well, is if its possible to have column titles without selecting from a range of cells like :

Code:
    With frmAuctionProject.ListBoxBids
        .ColumnHeads = True
        .RowSource = Cells.Range("a2:a7")
    End With

can i change the rowsource somehow and just type in the title i want? just i dont want anything in the actual spreadsheet.

Thank u again!
 
Upvote 0
Sorry no. To have column headers you have to assign a range to the RowSource property, and you can't use AddItem if that's the case (and vice versa). You could put some labels above the ListBox.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top