Solved

Excel vba - specify bound column 2 in List Box

Posted on 2012-03-28
2
421 Views
Last Modified: 2012-03-28
Hello.  I have this handy piece of code which allows me to multi-select values in a list box, and those multiple values store in a cell as a comma separated string.

The problem is this only seems to work assuming the column you want bound is the same column that the list box shows (in this case column 1).  How can I get this code to work if I want the column 1 to show in the list box, but column 2 bound with the list of comma separated values?
Sub ListBox1_LostFocus()
  Dim s As String, i As Integer

  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) = True Then s = s & .List(i) & ","
    Next i
  End With
  
  With Range("A1")
    If s = vbNullString Then
      .Value = vbNullString
      Exit Sub
      Else
      .Value = Left(s, Len(s) - 1)
    End If
    On Error Resume Next
    '.Comment.Delete
    '.AddComment .Value
  End With
  
End Sub

Open in new window


Attached is an Excel file containing the code, list box, and named column range.
Multi-Select-in-Excel-List-Box.xlsm
0
Comment
Question by:jobprojn
[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 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 37779599
Just add a second parameter to the .List

Sub ListBox1_LostFocus()
  Dim s As String, i As Integer

  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) = True Then s = s & .List(i, 1) & ","     'Note the .List(i,1)
    Next i
  End With
  
  With Range("A1")
    If s = vbNullString Then
      .Value = vbNullString
      Exit Sub
      Else
      .Value = Left(s, Len(s) - 1)
    End If
    On Error Resume Next
    '.Comment.Delete
    '.AddComment .Value
  End With
  
End Sub

Open in new window

0
 

Author Closing Comment

by:jobprojn
ID: 37779646
Worked like a charm.  Thanks!!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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 …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

756 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