Solved

How to change listbox entries via Excel 2010 VBA

Posted on 2013-11-18
8
399 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 47

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 47

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 47

Expert Comment

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

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 47

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

751 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