Issue when trying to select a line of a ListBox control via VBA code

Paulo75

New Member
Joined
Oct 5, 2013
Messages
2
Hello,

My problem is I cannot select dynamically a line of a ListBox control with VBA code.

To be precise, I have a form called myForm, which contains a ListBox control called myListBox. This ListBox has multiple columns and only one line can be selected at a time.

If I click in myListBox, then I select a line which becomes highlighted. Everything normal.

Then, if I put this piece of code :
Code:
[FONT=Helvetica]myForm.Hide[/FONT]
[FONT=Helvetica]// some instructions...[/FONT]
[FONT=Helvetica]// ...[/FONT]
[FONT=Helvetica]myForm.Show[/FONT]
the previously selected line of myListBox is no more selected (highlighted)

If I modify slightly the code above with :
Code:
[FONT=Helvetica]myForm.Hide[/FONT]
[FONT=Helvetica]// some instructions...[/FONT]
[FONT=Helvetica]// ...[/FONT]
[FONT=Helvetica]myForm.myListBox.ListIndex = 10[/FONT]
[FONT=Helvetica]myForm.Show[/FONT]
then I can get the 10th line selected and highlighted. But obviously, the 10th line is not necessarily the last line I selected, as you might guess.

This is where I point an issue : before executing the instruction myForm.Show, the property myListBox.ListIndex is well set to the last line I selected but there is no way to retrieve this state once the form displayed.

As a conclusion, this code :
Code:
[FONT=Helvetica]myForm.Hide[/FONT]
[FONT=Helvetica]// some instructions...[/FONT]
[FONT=Helvetica]// ...[/FONT]
[FONT=Helvetica]Dim myLine as Integer[/FONT]
[FONT=Helvetica]myLine = 10[/FONT]
[FONT=Helvetica]myForm.myListBox.ListIndex = myLine[/FONT]
[FONT=Helvetica]myForm.Show[/FONT]
highlights the 10th line.

But this code :
Code:
[FONT=Helvetica]myForm.hide[/FONT]
[FONT=Helvetica]// some instructions...[/FONT]
[FONT=Helvetica]// ...[/FONT]
[FONT=Helvetica]Dim myLine as Integer[/FONT]
[FONT=Helvetica]myLine = myForm.myListBox.ListIndex //myLine takes the right value[/FONT]
[FONT=Helvetica]myForm.myListBox.ListIndex = myLine[/FONT]
[FONT=Helvetica]myForm.show[/FONT]
does not.

Has someone ever encountered this behavior?
I thank you very much in advance!

Bye,
Paulo
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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