preselect listbox item

greg kramer

New Member
Joined
Dec 13, 2009
Messages
24
I know that you can select an item or preselect an item with the following syntax:

listbox.selected(1) = true

but how would one convert the 1 to text from another source so that it would work the same way

that is,

listbox.selected("word")=true

probably not formulating the question very well but any thoughts would be appreciated.
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
Office Version
  1. 365
Platform
  1. Windows
It the value matches an item in the list and the listbox is not multiselect then you should be able to do this.
Code:
Listbox1.Value = "Word"
If it's not on the list or the listbox is multiselect then you'll get an error, something like 'Invalid property value'.
 

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
500
Astr=Listbox1.list(i) - this gives you the text - is that your question?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

What are you trying to do? Where are the values you want to reject coming from?
 

greg kramer

New Member
Joined
Dec 13, 2009
Messages
24
Rasm\Norie-

Thanks for responding.

The list box is populated from a listfillrange on another sheet.

In a sperate area, there is a default value for this list in a cell that I would like to use to preselect it in the multiselect listbox.

Guess we can't post screen shots on the board...shame, picture is worth 1000k words.

Regards,

Greg
 

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
500

ADVERTISEMENT

you can add items to your list


ListBox1.additem "YourCellValue"

to clear the lsitbox use ListBox1.clear

I assume this is an activeX control

If you want to trigger an event when you click that cell then use this in ThisWorkBook

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

End Sub

The Target.Address will give you the coordiantes of the cell clicked - or use Target.Row or Target.Column - all depends on your needs.

Not sure if this helps
 

greg kramer

New Member
Joined
Dec 13, 2009
Messages
24
Thanks Rasm but I'm trying to select an item not add it.

This list is already fully populated.

When the users select the page they would have a default value highlighted in blue, somehow.

Which is why I was thinking it would be along the lines of:

listbox.selected(default value)=true
 

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
500
You have to refernc eit with an index - then they will be blue and selected.



i.e.
listbox.selected(0)=true
listbox.selected(4)=true


so do a loop

for i=0 to listbox1.listcount-1
if Listbox1.list(i) = "NameOfYourString" then ListBox1.selected(i)=True

next i


So something like that
 

greg kramer

New Member
Joined
Dec 13, 2009
Messages
24
Yes!

That's it.

Rasm, I owe you. Will send you a starbucks cards or something...please let me know.

Regards,

Greg
 

Watch MrExcel Video

Forum statistics

Threads
1,108,923
Messages
5,525,653
Members
409,658
Latest member
Yardcell

This Week's Hot Topics

Top