简体   繁体   中英

VBA- Printing to a listbox

I'm writing a piece of code in VBA that imports some data, organizes it, and then prints it out in a list box. But i can't figure out what i'm doing wrong when trying to print to the list box. My code looks like this.

Private Sub cmdDisplaySchedule_Click()
    Call listSchedule
End Sub

Public Sub listSchedule()

Dim i As Long

    For i = 1 To MB2Boats.Count 'MB2Boats is the class where all of my data is containted.

        SchedulerUserForm.LBoxBay2.AddItem
        SchedulerUserForm.LBoxBay2.List(i - 1, 0) = MB2Boats(i).MB2BoatName
        SchedulerUserForm.LBoxBay2.List(i - 1, 1) = MB2Boats(i).MB2Bay
        SchedulerUserForm.LBoxBay2.List(i - 1, 2) = MB2Boats(i).MB2Duration
        SchedulerUserForm.LBoxBay2.List(i - 1, 3) = MB2Boats(i).MB2Waiting
        SchedulerUserForm.LBoxBay2.List(i - 1, 4) = MB2Boats(i).MB2StandardDeviation
    Next i

End Sub

The above will only print the boatnames and nothing else. Any help is appreciated. Thanks!

Maybe all data is loaded into the form and you just don't see it yet. Try adding this line to the end of the sub:

SchedulerUserForm.LBoxBay2.ColumnCount = 5

As an update to the comments provided here is an interesting little example (with an empty workbook an empty new UserForm1 and and empty new ListBox1 dropped into the UserForm1:

Public Sub ListBoxColumnTest()

Load UserForm1

UserForm1.ListBox1.AddItem
UserForm1.ListBox1.List(0, 0) = "hello"
UserForm1.ListBox1.List(0, 1) = "hello"
UserForm1.ListBox1.List(0, 2) = "hello"
UserForm1.ListBox1.List(0, 3) = "hello"
UserForm1.ListBox1.List(0, 4) = "hello"

'The below message box will answer with 1
MsgBox "The current column count is: " & UserForm1.ListBox1.ColumnCount
'This will increase the column count to 5 and
'thereby only adjust the column widths (fair distribution of the give space for 5 even columns).
UserForm1.ListBox1.ColumnCount = 5

UserForm1.Show

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM