Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to change listbox entries via Excel 2010 VBA

Posted on 2013-11-18
8
Medium Priority
?
404 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
[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
  • 5
  • 3
8 Comments
 
LVL 49

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 49

Accepted Solution

by:
Martin Liss earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 49

Expert Comment

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

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 49

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

705 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