Solved

ListBox in excel vba

Posted on 2013-01-16
4
768 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 12

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

749 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