PDA

View Full Version : listbox



michaelhayes
08-24-2007, 01:21 AM
hey all,

first let me say its great to have vba help online. im doing an online course on this so its really hard. i am using microsoft word and wanted to make a listbox of the days of the week. so far i have come up with this but it isnt working. can anybody here help me out or refer to somewhere that helps beginners with vba stuff?

btw i called my listbox lbxDay

thanks in advance



Private Sub lbxDay_Initialize()
listlbxDay.AddItem "Monday"
listlbxDay.AddItem "Tuesday"
listlbxDay.AddItem "Wednesday"
listlbxDay.AddItem "Thursday"
listlbxDay.AddItem "Friday"
listlbxDay.AddItem "Saturday"
listlbxDay.AddItem "Sunday"
End Sub

SOS
08-24-2007, 01:34 AM
michaelhayes

What you need is:



Private Sub lbxDay_Initialize()
With lbxDay
.AddItem "Monday"
.AddItem "Tuesday"
.AddItem "Wednesday"
.AddItem "Thursday"
.AddItem "Friday"
.AddItem "Saturday"
.AddItem "Sunday"
End With
End Sub


Hope that helps

Seamus

michaelhayes
08-24-2007, 03:40 AM
i tried it but it didnt work??????? so cofnused...

SOS
08-24-2007, 04:03 AM
michael,

Sorry, is the listbox on a Userform?

If so then the code needs to go inside a UserForm_Initialize sub:



Sub UserForm_Initialize
With lbxDay
.AddItem "Monday"
.AddItem "Tuesday"
.AddItem "Wednesday"
.AddItem "Thursday"
.AddItem "Friday"
End With


Hope this helps

Seamus

mdmackillop
08-24-2007, 08:56 AM
Using loops can save typing, and no doubt introduces you to some new terms!

Option Explicit
Sub UserForm_Initialize()
Dim i As Long
For i = 1 To 7
With lbxday
.AddItem WeekdayName(i, , 2)
End With
Next
End Sub

fumei
08-24-2007, 11:27 AM
Ummmmmmm, where did you get:

Sub lbxDay_Initialize()

???????

There is no Initialize event for a Listbox, either listbox on a userform, or a ActiveX listbox in a document.

SOS - while quite correct that it is better to use a With statement, tectnically speaking the original multiple instructions of .AddItems is valid. The syntax is perfectly correct. The problem is the Sub lbxDay_Initialize() is never going to fire!

And, of course, Malcom's suggestion is best of all.