Solved

ListBox in excel vba

Posted on 2013-01-16
4
781 Views
Last Modified: 2013-01-16
I have a listbox in my code in which I am trying to select an item with vba to set a varaible equal to that selection.  I get a null value.  The listbox is popluated and code does select that appropriate item, but still I get null value.

Code:
    Dim ItemCount As Integer: ItemCount = Group1_List.ListCount
    Dim CurrentItem As String
   
    If Group1_List.ListCount <> 0 Then
        For ListItem = 0 To ItemCount - 1
            Group1_List.Selected(ListItem) = True
            CurrentItem = Group1_List.Value
                                   
        Next ListItem
    End If
0
Comment
Question by:rckrch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 38784824
I think you want to test to see if an item is selected.

The line of code Me.ListBox1.Selected(ListItem) = True selects the value, I do not think that you want that to happen.



Dim CurrentItem As String

Dim bSelected As Boolean
bSelected = False
    
    If Me.ListBox1.ListCount <> 0 Then
        For ListItem = 0 To ItemCount - 1
            If Me.ListBox1.Selected(ListItem) = True Then
              CurrentItem = Me.ListBox1.Value
              bSelected = True
            End If
        Next ListItem
    End If

If bSelected Then
  MsgBox CurrentItem
End If

Open in new window

0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 400 total points
ID: 38784858
You can just use Group1_List.list(Group1_List.listindex)
0
 

Author Closing Comment

by:rckrch
ID: 38785103
Thanks.  This worked perfectly.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 38785192
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question