Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default How to get the index in VBA of the selected item in a ListBox

I'd like to get the index of the selected item in a ListBox in VBA.
In real VB I do it with the following code:

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long

Private Sub UserForm_Initialize()
Dim Counter As Long

ListBox1.Clear

For Counter = 1 To 20
ListBox1.AddItem "MyItem " & Counter
Next

CommandButton1.Caption = "Index of selected Item"
End Sub

Private Sub CommandButton1_Click()
MsgBox (SendMessage(ListBox1.hWnd, &H19F, 0, 0))
End Sub

But how can I do it in VBA (e.g. Excel)?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to get the index in VBA of the selected item in a ListBox

Just use the ListIndex property of the ListBox control. It will be -1 if no
item is selected. It is the same as real VB. Why do you jump through the
hoops with SendMessage in the first place?


With Me.ListBox1
If .ListIndex < 0 Then
Debug.Print "No item selected"
Else
Debug.Print "Item: " & CStr(.ListIndex), .List(.ListIndex)
End If
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Stefan Mueller" wrote in message
ups.com...
I'd like to get the index of the selected item in a ListBox in VBA.
In real VB I do it with the following code:

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long

Private Sub UserForm_Initialize()
Dim Counter As Long

ListBox1.Clear

For Counter = 1 To 20
ListBox1.AddItem "MyItem " & Counter
Next

CommandButton1.Caption = "Index of selected Item"
End Sub

Private Sub CommandButton1_Click()
MsgBox (SendMessage(ListBox1.hWnd, &H19F, 0, 0))
End Sub

But how can I do it in VBA (e.g. Excel)?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default How to get the index in VBA of the selected item in a ListBox

chip:

what would be the difference in using either of these? they both seem to return
the same result.

?.listbox1.List(.listbox1.ListIndex))

?.listbox1.Value

--


Gary


"Chip Pearson" wrote in message
...
Just use the ListIndex property of the ListBox control. It will be -1 if no
item is selected. It is the same as real VB. Why do you jump through the hoops
with SendMessage in the first place?


With Me.ListBox1
If .ListIndex < 0 Then
Debug.Print "No item selected"
Else
Debug.Print "Item: " & CStr(.ListIndex), .List(.ListIndex)
End If
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Stefan Mueller" wrote in message
ups.com...
I'd like to get the index of the selected item in a ListBox in VBA.
In real VB I do it with the following code:

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long

Private Sub UserForm_Initialize()
Dim Counter As Long

ListBox1.Clear

For Counter = 1 To 20
ListBox1.AddItem "MyItem " & Counter
Next

CommandButton1.Caption = "Index of selected Item"
End Sub

Private Sub CommandButton1_Click()
MsgBox (SendMessage(ListBox1.hWnd, &H19F, 0, 0))
End Sub

But how can I do it in VBA (e.g. Excel)?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default How to get the index in VBA of the selected item in a ListBox

There is no difference in the result. The former is most useful when
retrieving a secondary column from the list.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
chip:

what would be the difference in using either of these? they both seem to
return the same result.

?.listbox1.List(.listbox1.ListIndex))

?.listbox1.Value

--


Gary


"Chip Pearson" wrote in message
...
Just use the ListIndex property of the ListBox control. It will be -1 if
no item is selected. It is the same as real VB. Why do you jump through
the hoops with SendMessage in the first place?


With Me.ListBox1
If .ListIndex < 0 Then
Debug.Print "No item selected"
Else
Debug.Print "Item: " & CStr(.ListIndex), .List(.ListIndex)
End If
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Stefan Mueller" wrote in message
ups.com...
I'd like to get the index of the selected item in a ListBox in VBA.
In real VB I do it with the following code:

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long

Private Sub UserForm_Initialize()
Dim Counter As Long

ListBox1.Clear

For Counter = 1 To 20
ListBox1.AddItem "MyItem " & Counter
Next

CommandButton1.Caption = "Index of selected Item"
End Sub

Private Sub CommandButton1_Click()
MsgBox (SendMessage(ListBox1.hWnd, &H19F, 0, 0))
End Sub

But how can I do it in VBA (e.g. Excel)?






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default How to get the index in VBA of the selected item in a ListBox

ok, i see.
thanks, bob

--


Gary


"Bob Phillips" wrote in message
...
There is no difference in the result. The former is most useful when
retrieving a secondary column from the list.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
chip:

what would be the difference in using either of these? they both seem to
return the same result.

?.listbox1.List(.listbox1.ListIndex))

?.listbox1.Value

--


Gary


"Chip Pearson" wrote in message
...
Just use the ListIndex property of the ListBox control. It will be -1 if no
item is selected. It is the same as real VB. Why do you jump through the
hoops with SendMessage in the first place?


With Me.ListBox1
If .ListIndex < 0 Then
Debug.Print "No item selected"
Else
Debug.Print "Item: " & CStr(.ListIndex), .List(.ListIndex)
End If
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Stefan Mueller" wrote in message
ups.com...
I'd like to get the index of the selected item in a ListBox in VBA.
In real VB I do it with the following code:

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long

Private Sub UserForm_Initialize()
Dim Counter As Long

ListBox1.Clear

For Counter = 1 To 20
ListBox1.AddItem "MyItem " & Counter
Next

CommandButton1.Caption = "Index of selected Item"
End Sub

Private Sub CommandButton1_Click()
MsgBox (SendMessage(ListBox1.hWnd, &H19F, 0, 0))
End Sub

But how can I do it in VBA (e.g. Excel)?








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
Need selected item from listbox after double click [email protected] Excel Programming 1 February 23rd 06 03:18 PM
Add item to listbox, but only if it is not already there. Mark Excel Programming 4 September 2nd 05 10:07 PM
How to copy selected listbox item to sheet michdan Excel Programming 1 July 14th 05 12:49 PM
Listbox - write newly selected item back to the worksheet Cheryl Excel Programming 0 July 23rd 04 01:56 PM
Count number of selected item in Listbox Todd Huttenstine Excel Programming 4 June 14th 04 06:54 PM


All times are GMT +1. The time now is 11:20 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"