Can't get a value out of a list box

J

JonR

Hi
I have a userform that allows users to select and print various objects. I
am trying to incorporate a listbox that allows the user to select the number
of copies desired.

I can populate the listbox fairly easily, but when I try to run the print
program (or even a test MsgBox) I get an error stating that the value of the
variable nCopies is null.

How do I set this up so that the variable nCopies gets its value from the
list box?

Example code is below; the real deal jumps around a bit between several
different subs.


sub load ()
' populates the list box
Dim num As Integer
For num = 1 To 25
Form1.ListBox1.AddItem (num)
Next

end sub

sub printcopy ()
Dim nCopies as Integer
nCopies = Form1.ListBox1.Value
for i= 1 to nCopies
ActiveSheet.PrintOut
next
end sub
 
T

Tom Ogilvy

The usual way to do what you want is to use a spin button and a textbox.
Place the spinbutton next to the textbox.

Private Sub SpinButton1_Change
Textbox1.Value = SpinButton1.Value
End Sub

Set the spinbutton properties with a min of 0 and a max of 25

Then just read the value of the textbox.
 
S

sebastienm

JonR,
Have you tried :
Form1.Listbox1.ListIndex=0
once all your items are added. It will select the first item.

Or maybe there is something i don't understand when you say : "the number is
automatically selected as you scroll through.".

In the Print dialog, it is not a list box that is used, but a SpinButton
(from the Control Toolbox toolbar, with its Orientation property set to
frmOrientationVertical) which is linked to textbox (displying the Spinbutton
value). Look at the Spinbutton control online help, they have an example.
I'll get back in a minute with it.

Regards,
Seb
 
J

JonR

Hi Sebastien

Tom Ogilvy answered my question with the SpinButon control, just like you
suggested. Thanks for the help.

Jon
 
Top