Solved

How to change listbox entries via Excel 2010 VBA

Posted on 2013-11-18
8
392 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 45

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 45

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
 

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

 
LVL 45

Expert Comment

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

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 45

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

707 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