Solved

ListBox in excel vba

Posted on 2013-01-16
4
736 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 46

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

861 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

23 Experts available now in Live!

Get 1:1 Help Now