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
Solved

Excel vba - specify bound column 2 in List Box

Posted on 2012-03-28
2
412 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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.

808 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