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

x
?
Solved

Extended listbox selections to cells

Posted on 2014-07-23
5
Medium Priority
?
157 Views
Last Modified: 2014-07-24
Hi Experts,

I have a list box (Countrylst) that has names of countries in it. I need to be able to select Multiple countries so have set its selection type to 'extended' (it is on a worksheet - 'Location' in an excel file).

I need to get the countries that have been selected into a cell so that I can use the country names elsewhere in the workbook. Obviously when I have the selection type set to 'single' I can get a number reference for the selection (eg cell N1 returns Cambodia if it has been selected). How do I get the results when the listbox is extended?

I'm sure this is possible but not sure where the code needs to go or what it needs to do...

Thanks in advance for your help!
0
Comment
Question by:martywal
[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
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Steve
ID: 40216125
You say you want the selections in a cell, how would you see it formated?
Would they be comma seperated?
With all in one cell would they be useful, or would them being in all cells from a set cell down be better?
0
 
LVL 34

Expert Comment

by:Norie
ID: 40216311
Which type of listbox is it, Forms or ActiveX?
0
 
LVL 34

Accepted Solution

by:
Norie earned 2000 total points
ID: 40216324
This code is for a control from the Forms toolbox.
Sub GetAllSelectedFromFormsListBox()
Dim lst As Object
Dim I As Long
Dim arrSelected()
Dim cnt As Long
Dim Delim As String

    Delim = ","

    Set lst = [Countrylst]

    For I = 1 To [Countrylst].ListCount
        If [Countrylst].Selected(I) Then
            ReDim Preserve arrSelected(cnt)

            arrSelected(cnt) = [Countrylst].List(I)

            cnt = cnt + 1
        End If
    Next I

    If cnt > 0 Then

        ' put in cell as delimited list
        Range("B1").Value = Join(arrSelected, Delim)

        ' put in row
        Range("D1").Resize(, UBound(arrSelected) + 1).Value = arrSelected

        ' put in column
        Range("B3").Resize(UBound(arrSelected) + 1).Value = Application.Transpose(arrSelected)
        
    End If

End Sub

Open in new window


The code for an ActiveX listbox will be similar, with some changes in the loop.
0
 

Author Comment

by:martywal
ID: 40218300
That's great thanks so much for your help on this one!!!
0
 

Author Closing Comment

by:martywal
ID: 40218301
Quick and accurate response.
Gotta love Experts Exchange!!!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

721 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