Constant execution of ListBox.Click

  • Thread starter Thread starter Curious George
  • Start date Start date
C

Curious George

How do I constantly make events fire, like ListBox.Click subroutine.
Currently I can run the result only by executing the macro run button
thingie. Cant I make the VBA work all the time without pressing any
buttons to run the code?
 
ListBox_Click will run when and only when a different listbox selection is
made in this very listbox. That is the whole point of events. If you want
sonething to happen all the time then program it to happen all the time,
don't use events.

HTH. Best wishes Hrald
 
Personally, I like having to click the button after making the selection in the
listbox, but maybe you could use a different event.

I created a userform with a listbox and then used the _MouseUp event.

Option Explicit
Private Sub ListBox1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
MsgBox Me.ListBox1.Value
Me.ListBox1.ListIndex = -1
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.ListBox1
For iCtr = 1 To 5
.AddItem "asdf" & iCtr
Next iCtr
End With
End Sub
 
Harald said:
ListBox_Click will run when and only when a different listbox selection
is made in this very listbox. That is the whole point of events. If you
want sonething to happen all the time then program it to happen all the
time, don't use events.

HTH. Best wishes Hrald

I want the code to execute exactly when the different selections are
made. But it just doesn't do it.
 
Dave said:
Personally, I like having to click the button after making the selection in the
listbox, but maybe you could use a different event.

I created a userform with a listbox and then used the _MouseUp event.

Option Explicit
Private Sub ListBox1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
MsgBox Me.ListBox1.Value
Me.ListBox1.ListIndex = -1
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.ListBox1
For iCtr = 1 To 5
.AddItem "asdf" & iCtr
Next iCtr
End With
End Sub

So using the toolbar and making just a simple listbox to the sheet wont
work, now I have to make a userform?
 
Nope.

I guessed you were using a userform. You didn't share enough details in the
first post.

I used a listbox from the control toolbox toolbar and only put the _mouseup
event in that worksheet's module.

Don't include the userform_initialize code.
 
Curious George said:
I want the code to execute exactly when the different selections are made.
But it just doesn't do it.

Ok, there are listboxes and there are listboxes. The ones from Forms toolbar
doesn't have events. Assign a standard macro to it and it runs on click.
The ones from the Controls toolbox will not fire the Click event if it is
set to multiselect, use the Change event if so.

HTH. Best wishes Harald
 
Back
Top