ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Listbox Initial Values (https://www.excelbanter.com/excel-programming/414876-listbox-initial-values.html)

Marvin Buzz

Listbox Initial Values
 
I have two listbox controls named FROM and TO
The rowsource for both is the same, but variable in length. When the user
form loads, I would like to have FROM display and select the first value in
the rowsource, and to have TO display and select the last value in the
rowsource. Using TOPINDEX causes the correct value to be displayed, but the
returned value is null.

Any suggestions please?

Thanks.

Marvin


JLGWhiz

Listbox Initial Values
 
If you have only one column in the ListBox and the BoundColumn is set to 1,
then you can use the Value property to return the displayed value.

x = UserForm1.ListBox1.Value
MsgBox x

Would display the selected value. Also see the Selected property in VBA help.

"Marvin Buzz" wrote:

I have two listbox controls named FROM and TO
The rowsource for both is the same, but variable in length. When the user
form loads, I would like to have FROM display and select the first value in
the rowsource, and to have TO display and select the last value in the
rowsource. Using TOPINDEX causes the correct value to be displayed, but the
returned value is null.

Any suggestions please?

Thanks.

Marvin


JLGWhiz

Listbox Initial Values
 
To return the last item in the list box.

y = UserForm1.ListBox1.Selected(ListBox1.Rows.Count -1)
MsgBox y

ListBox row index begins with zero, so the last item index number will be
one less that the number of rows.


"Marvin Buzz" wrote:

I have two listbox controls named FROM and TO
The rowsource for both is the same, but variable in length. When the user
form loads, I would like to have FROM display and select the first value in
the rowsource, and to have TO display and select the last value in the
rowsource. Using TOPINDEX causes the correct value to be displayed, but the
returned value is null.

Any suggestions please?

Thanks.

Marvin


Marvin Buzz

Listbox Initial Values
 
Maybe my requirement wasn't clear. I want the FROM box to display the first
item from rowsource, and the TO box to display the last. Additionally, the
values to be returned from the controls must be the first and last
respectively.

Marvin

"JLGWhiz" wrote:

To return the last item in the list box.

y = UserForm1.ListBox1.Selected(ListBox1.Rows.Count -1)
MsgBox y

ListBox row index begins with zero, so the last item index number will be
one less that the number of rows.


"Marvin Buzz" wrote:

I have two listbox controls named FROM and TO
The rowsource for both is the same, but variable in length. When the user
form loads, I would like to have FROM display and select the first value in
the rowsource, and to have TO display and select the last value in the
rowsource. Using TOPINDEX causes the correct value to be displayed, but the
returned value is null.

Any suggestions please?

Thanks.

Marvin



All times are GMT +1. The time now is 01:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com