excel form listbox on click select first item

Pancras

New Member
Joined
Feb 2, 2018
Messages
2
i have a listbox with items. when i select an item(the second for example), the listbox items get resorted and the first item should then be selected. what happens is that the second item stays selected.
what i have so far:
form:
listbox(listbox1)
button(Commandbutton1)
button(Commandbutton2)
button(Commandbutton3)
the code:
Option Explicit
Private boo As Boolean
Private Sub UserForm_Initialize()
f1
boo = True
End Sub
Private Sub CommandButton1_Click()
f1
End Sub
Private Sub CommandButton2_Click()
f2
boo = False
Me.ListBox1.Selected(0) = True ' this fires ListBox1_Click. Boo ensures that ListBox1_Click's code is not executed
boo = True
End Sub
Private Sub CommandButton3_Click()
MsgBox Me.ListBox1
End Sub
Private Sub ListBox1_Click()
If boo Then
Debug.Print Me.ListBox1, "click"
CommandButton2_Click
End If
End Sub
Sub f1()
With Me.ListBox1
.Clear
.AddItem "AAP"
.AddItem "NOOT"
.AddItem "MIES"
End With
End Sub
Sub f2()
With Me.ListBox1
.Clear
.AddItem "NOOT"
.AddItem "MIES"
.AddItem "AAP"
End With
End Sub

When i click button2 everything works as expected(the first item gets selected) and button 3 shows the correct value(item1)
When i select the second item in the listbox, the second item stays selected. Expected behaviour should be 1st gets selected. button3 throws error(there is a null) (i could show debug.print listbox1.columns(0,0) but that is not the point.
thanks in advance,
pancras
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

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