Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Excel vba - specify bound column 2 in List Box

Posted on 2012-03-28
2
Medium Priority
?
508 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

647 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