Solved

ListBox in excel vba

Posted on 2013-01-16
4
721 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
  • 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 45

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 45

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now