Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default List Box - Select an element?

Hello I'm just wondering if it's possible to get the program to select a
value in a list box,

I have a lot of different codes all within a list box, and sometimes it's
quite hard to find a particular one. I'm just wondering if it'd be possible
to let the user type in a code and then this automatically highlight the
found code within the list box?

Thanks for any help in advance!

Nathan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default List Box - Select an element?

Hi Nate,

Put a ListB ox from the Controls Toolbox
on a worksheet.

In a standard module (see below), paste
the following code:

'==========
Option Explicit

Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim arr As Variant
Dim LBox As OLEObject

Set WB = ThisWorkbook
Set SH = WB.Sheets("Sheet1") '<<====CHANGE
Set LBox = SH.OLEObjects("Listbox1") '<<====CHANGE

arr = VBA.Array("Blue", "Green", _
"Yellow", _
"Brown", _
"Grey", _
"Lilac", _
"Pink") '<<====CHANGE

Set LBox = SH.OLEObjects("Listbox1")

With LBox.Object
.Clear
.List = arr
End With

End Sub
'<<==========

Alt-F11 to open the VBA Editor
Menu | Insert | Module
Paste the above code
Alt-F11 To return to Excel

In the worksheet module (see below),
paste the following code:

'==========
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Dim oleObj As OLEObject
Dim i As Long

Set oleObj = Me.OLEObjects("Listbox1") '<<====CHANGE
Set Rng = Me.Range("A1") '<<==== CHANGE

Set Rng = Intersect(Rng, Target)

If Not Rng Is Nothing Then
With oleObj.Object

For i = 0 To .ListCount - 1
If StrComp(.List(i), Rng.Value, _
vbTextCompare) = 0 Then
.ListIndex = (i)
Exit For
End If
Next i
End With
End If
End Sub
'<<==========

The above code is worksheet event code
and should be pasted in the module behind
the worksheet:

Right-Click the worksheet tab
Select 'View Code'
Paste

Now, entering any of the ListBox values in
cell A1, on the sheet of interest, will select
that value in the ListBox.




---
Regards.
Norman


"NateBuckley" wrote in message
...
Hello I'm just wondering if it's possible to get the program to select a
value in a list box,

I have a lot of different codes all within a list box, and sometimes it's
quite hard to find a particular one. I'm just wondering if it'd be
possible
to let the user type in a code and then this automatically highlight the
found code within the list box?

Thanks for any help in advance!

Nathan


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default List Box - Select an element?

Hi Nate,

Alternatively, and perhaps more simply:

- replace the ListBox with a ComboBox
from the Control Toolbox

- dispense with the code in the worksheet
module

- replace the code suggested for the
standard module with the folloing version

Now, using the ComboBox's MatchEntry
property, as the user types each letter, the
control will sugggest a value.


---
Regards.
Norman
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rules for element-by-element product in array multiplication Paul Excel Programming 2 March 22nd 08 11:42 PM
DATA VALIDATION LIST- CAN I SELECT FROM LIST WITHOUT SCROLLING Bellyjean Excel Worksheet Functions 1 March 7th 08 09:46 PM
The Kth element in a text list kjs Excel Worksheet Functions 15 February 7th 07 10:59 PM
how to randomly select a name in a list and then the select the ne [email protected] Excel Worksheet Functions 1 September 20th 06 08:09 AM
Last list item selected in a Multi-Select list box? Brian Excel Programming 3 December 5th 05 09:12 PM


All times are GMT +1. The time now is 12:07 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"