Solved

How to change listbox entries via Excel 2010 VBA

Posted on 2013-11-18
8
395 Views
Last Modified: 2013-11-20
below is the code that I use to retrieve a list of items from the .xls
 I'd like this list to be the source of List Box 52

My code returns the correct list (via the msgbox)
however returns an error when I try to assign it to the rowsource of List Box 52



Sub ListBox53_Change()
Dim i, dept As Integer
Dim DeptRowSource As String
With Worksheets("triangles").ListBoxes("List Box 53")
    MsgBox (.ListCount)
    MsgBox (.Selected(1))
'    For i = 0 To .ListCount - 1
        'If .Selected(i) Then
            'msg = msg & .List(i) & vbNewLine
        'End If
    'Next i
End With
'Adjust Department List Box
DeptRowSource = ""
For dept = 1 To Worksheets("Vars").Range("ListBoxItems_Dept_Genius").Rows.Count
    If Worksheets("Vars").Range("AF5").Offset(dept - 1, 2) > 0 Then
        DeptRowSource = DeptRowSource & Worksheets("Vars").Range("AF5").Offset(dept - 1, 1) & ";"
        End If
Next dept
MsgBox (RowSource)
'Update list of depts based on Company Selection
Worksheets("triangles").ListBoxes("List Box 52").RowSource = DeptRowSource

End Sub


Also side Q - how do I change the names of the listboxes (without making them ActiveX listboxes)?

thx
0
Comment
Question by:endurance
  • 5
  • 3
8 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39657873
To answer your second question first, in Design Mode right-click on the listbox and it's default or current name will appear in the little box just below the upper-left hand corner of the window. You then slect that name, type in wjat you want it called and press return.

To help you with your first question I think I'll need your workbook.
0
 

Author Comment

by:endurance
ID: 39657905
thanks,
though I think I can simplify the question:
See attached .xls
I get an error when I run the "ChangeSourceofLB1" macro to set the rowsource of the multiselect listbox
Test---populate-listbox.xlsm
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39658025
Here is a solution. It includes this change to your code
Sub ChangeSourceofLB1()
'Worksheets("triangles").ListBoxes("List Box 1").rowsource = "toys;football;"
Dim strNew As String
Dim lngLastRow As Long
strNew = InputBox("Please enter a new value for the listbox")
With Worksheets("Sheet2")
    lngLastRow = .Range("A65536").End(xlUp).Row
    .Cells(lngLastRow + 1, 1).Value = strNew
End With
End Sub

Open in new window

and a list on Sheet2 that contains the names. I used the Names manager to create a Named Range called 'ListboxEntries' and the formula associated with the Named Range makes it dynamic. I also set the InputRange for the listbox to that named range.
Q-28297485.xlsm
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:endurance
ID: 39660046
Thanks - that will work, though is there a way to directly assign the source of the listbox to a string.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39660080
Don't take this as a definitive answer but I don't think so.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39660123
.AddItem("add a value")
0
 

Author Comment

by:endurance
ID: 39660991
don't want to just add items - as I need to redefine the list (as the actual use is to also take items off of the list)...

also - I saw online the use of "RowSource = "
I see that it didn't work for me, just curious what are people referring to, and why didn't it work for my usage
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39661108
I believe RowSource works when you reference an Access table but then again I'm not sure.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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;…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

815 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

12 Experts available now in Live!

Get 1:1 Help Now