Solved

How to change listbox entries via Excel 2010 VBA

Posted on 2013-11-18
8
402 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 48

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 48

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
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.

 

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 48

Expert Comment

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

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 48

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

635 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