PDA

View Full Version : Solved: Populating a listbox



austenr
05-03-2010, 12:17 PM
Having trouble getting this started for some reason.

User enters a number in a text box. Next presses a button and populates a list box with the following:

The number multiplied 12 times.

Example,

2 X 1 = 2
2 X 2 = 4
......
2 X 12 = 24

The number entered in this case would be 2.

I can do the for next loop just cant figure out how to populate the list box. Thabks.

GTO
05-03-2010, 12:40 PM
Greetings Austen,Presuming a userform:


Private Sub UserForm_Initialize()

Const MULTIPLIER As Long = 2
Const START_VAL As Long = 1

Dim Factor As Long

For Factor = START_VAL To START_VAL + 11
Me.ListBox1.AddItem MULTIPLIER * Factor
Next
End Sub

Hope that helps,

Mark

Edit: Yikes! Somehow managed to get everything on one really long line...

austenr
05-03-2010, 12:45 PM
That helps but using the constant has me a bit confused. In the text box I mentioned above you could input any number. Can you explain?

GTO
05-03-2010, 12:54 PM
Sorry Austen - I read too quickly and glossed over the textbox. How about:


Private Sub CommandButton1_Click()
Dim StartVal As Long
Dim i As Long

Me.ListBox1.Clear

StartVal = Me.TextBox1.Value

For i = 1 To 12
Me.ListBox1.AddItem StartVal * i
Next

End Sub


Mark

austenr
05-03-2010, 05:31 PM
Thanks that got it.