Command button caption to show what is selected in the listbox.

danbates

Board Regular
Joined
Oct 8, 2017
Messages
240
Hi,

Please can someone help me?

I would like my commandbutton1 caption to show what is selected in my listbox1.

Any help would be appreciated.

Thanks

Dan
 

James006

Well-known Member
Joined
Apr 4, 2009
Messages
3,680
Hi,

Your description is very vague ...

There are several questions one would like to ask ...

Can the following be of any help :

Code:
CommandButton1.Caption = ListBox1.Selected(0)
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,013
Assuming that your listbox allows only a single selection, try...

Code:
    With Me.ListBox1
        Me.CommandButton1.Caption = .List(.ListIndex)
    End With
Note, you should check to make sure that a selection from the listbox has been made before trying to set the caption for the commandbutton. When no selection is made, ListIndex is equal to -1, so you can test as follows...

Code:
If Me.ListBox1.ListIndex <> -1 Then
Hope this helps!
 

danbates

Board Regular
Joined
Oct 8, 2017
Messages
240
Hi,

James006 - Sorry I was vague in my description. Your code shows 'false' on my command button when I make a selection from my listbox.

Domenic - Your code works just how I wanted, thanks.

Thank you both for taking time to help me out.

Regards

Dan
 

Forum statistics

Threads
1,082,610
Messages
5,366,601
Members
400,906
Latest member
incanus

Some videos you may like

This Week's Hot Topics

Top