procedure 'Private sub <listbox>_Click()' runs only once

Gust Mariën

New Member
Joined
Jul 31, 2017
Messages
2
In an Vba program in an Excel Workbook I’m using a listbox with reference numbers to make one choice out (property MultiSelect = 0 – frmMultiSelectSingle) on an Userform. There’s a procedure ‘sub <Iistbox>_Click()’. I try to use the listbox several times in the program but it only enters the procedure once the first time an entry in the box is clicked on. The second time and after an entry is clicked on the program doesn’t enter the procedure anymore.
I can send / upload a workbook with the problem to make it clear.
Do you know how to tackle this problem?
Thanks for answering my question! I’m very grateful!
Kind regards,
Gust Mariën / the Netherlands
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Gust Marien,

Welcome to the Forum.

You may want to try to put your code in

Code:
Private Sub ListBox1_Click()

Instead of

Code:
sub_Click()

Be sure to change the "ListBox1" to the name of your ListBox.

I hope this helps...
 
Upvote 0
Thanks Igold!


I made a 'problem workbook' to explain the problem.
The program opens with the first Userform. clicking the button 'Generate random series' does so in a row on the worksheet and the frame 'Indexed series preferred?' activates. pushing 'Yes' lists the periods above each of the random numbers generated (already in the row above the series) in the list beneath. Clicking a period in the list indexes the random number series on the random number of that period and set the series in a column on the worksheet.
Here is the problem! The procedure 'Private Sub lstIdxJaar_Click()' executes only once.

Private Sub lstIdxJaar_Click()

mkIdxReeksen (lstIdxJaar.List(lstIdxJaar.ListIndex))
lstIdxJaar.Clear

Range("B6").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy

Range("S9").Select
If Not IsEmpty(Range("T9")) Then
Selection.End(xlToRight).Select
Set rangex = Selection
Else
Set rangex = Selection
End If
Range(Cells(9, rangex.Column + 1), Cells(9, rangex.Column + 1)).Select
' Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Invulbladcp.Hide

nogGrafofSluiten.Enabled = True
nogGrafofSluiten.knpNogGraf.Enabled = True
nogGrafofSluiten.knpEnd.Enabled = True
nogGrafofSluiten.Show

End Sub

Continuing my explanation. Pushing the the 'No' option in the frame 'Indexed series preferred?' sets the series in a column on the worksheet.

The button 'Another random series?' on the 2nd Userform activates the 1st Userform again and a new series can be generated. But, after indexing a series once, it is impossible the index another series because the procedure 'Private Sub lstIdxJaar_Click()' executes but once! How is that possible?

Thanks a lot if you can help me!

Now I show the userforms and the code directing the program.



Private Sub knpSeries_Click()
RndmReeks
fraIndex.Enabled = True
optIdxJa.Enabled = True
optIdxNee.Enabled = True
fraIndex.SetFocus

End Sub

Private Sub lstIdxJaar_Click()

mkIdxReeksen (lstIdxJaar.List(lstIdxJaar.ListIndex))
lstIdxJaar.Clear

Range("B6").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy

Range("S9").Select
If Not IsEmpty(Range("T9")) Then
Selection.End(xlToRight).Select
Set rangex = Selection
Else
Set rangex = Selection
End If
Range(Cells(9, rangex.Column + 1), Cells(9, rangex.Column + 1)).Select
' Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Invulbladcp.Hide

nogGrafofSluiten.Enabled = True
nogGrafofSluiten.knpNogGraf.Enabled = True
nogGrafofSluiten.knpEnd.Enabled = True
nogGrafofSluiten.Show



End Sub

Private Sub optIdxJa_Click()

vulLstIdxJaar

lstIdxJaar.Enabled = True
lstIdxJaar.SetFocus
' fraIndex.Enabled = False


End Sub
Private Sub optIdxNee_Click()
Dim rangex As Range

Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Range("A9").Select
If Not IsEmpty(Range("B9")) Then
Selection.End(xlToRight).Select
Set rangex = Selection
Else
Set rangex = Selection
End If
Range(Cells(9, rangex.Column + 1), Cells(9, rangex.Column + 1)).Select
' Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

Invulbladcp.Hide
nogGrafofSluiten.Enabled = True
nogGrafofSluiten.knpNogGraf.Enabled = True
nogGrafofSluiten.knpEnd.Enabled = True
nogGrafofSluiten.Show


End Sub

Private Sub userform_Initialize()


Invulbladcp.Enabled = True

knpSeries.Enabled = True
knpSeries.SetFocus

fraIndex.Enabled = False
optIdxJa = False
optIdxJa.Enabled = False
optIdxNee = False
optIdxNee.Enabled = False

End Sub




Private Sub knpEnd_Click()
nogGrafofSluiten.Hide
End
End Sub

' n o g G r a f o f S l u i t e n n o g G r a f o f S l u i t e n
Private Sub knpNogGraf_Click()

'1- initialize arrGraf

arrGraf = resetGraf

'2- initialize userform

'3- en nog even het aantal grafieken (nieuwe tabs in het grafiekenwboek)
' tellen

GrfTel = GrfTel + 1

' Invulblad.ActiveControl


Invulbladcp.Enabled = True


Invulbladcp.fraIndex.Enabled = False
Invulbladcp.optIdxJa.Enabled = False
Invulbladcp.optIdxNee.Enabled = False
Invulbladcp.optIdxJa = False
Invulbladcp.optIdxNee = False
Invulbladcp.knpSeries.Enabled = True
Invulbladcp.knpSeries.SetFocus

nogGrafofSluiten.Enabled = False
nogGrafofSluiten.Hide
Invulbladcp.Show


End Sub

And here is the in a module with procedures to let the program work.

Sub RndmReeks()

Range("B2").Select
ActiveCell.FormulaR1C1 = "=RAND()*1000"
Range("B2").Select
Selection.Copy
Range("C2:BI2").Select
ActiveSheet.Paste

Range("B2:BI2").Select
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False



End Sub
Sub vulLstIdxJaar()


Dim i As Integer


Dim sprong As Integer


With Invulbladcp.lstIdxJaar
For i = 1324 To 1384
.AddItem i
Next
End With

Invulbladcp.lstIdxJaar.ListIndex = -1
Invulbladcp.lstIdxJaar.Enabled = True
Invulbladcp.lstIdxJaar.SetFocus

End Sub


Sub mkIdxReeksen(idxjaar As Long)


Dim idxwr As Double ' jaarcijfer ophalen van het gekozen indexjaar
Dim tl As Integer ' doorloopt geselecteerde reeksen
Dim tlper As Integer ' doorloopt de periodes in de reeks
Dim klbegin As Long ' de beginkolom van de reeks in 'grafwb' bepalen



idxwr = ActiveSheet.Cells(2, idxjaar - 1322)

For tl = 2 To 61
ActiveSheet.Cells(6, tl) = (ActiveSheet.Cells(2, tl) / idxwr) * 100
Next



End Sub

Now I see that I didn't manage to show the pictures of the userforms. So probably my question gets not understandable.

If my question isn't understandable, is it possible to send the program to you?
Thanks once more!

Regards,

Gust
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top