You are here:

Excel/Multi select listbox

Advertisement


Question
Screen shot
Screen shot  
I am trying to create a code that will take items from a userform and place them in a cell sheet with the following format:

item1, item2, item3...ect

the current code I have is doing everything I want but I can not figure out how to get the items in one cell and in the format I am looking for.  Here is my current code:

Private Sub cmdOkay_Click()
   Dim i As Long, msg As String, Check As String, msgg As String
   With ListBox1
       For i = 0 To .ListCount - 1
         If .Selected(i) Then
         msg = msg & .List(i) & vbNewLine
         End If
       Next i
   End With
    
   If msg = vbNullString Then
       MsgBox "Please select task or Cancel"
       Exit Sub
   Else
       Check = MsgBox#"Selected Tasks:" & vbNewLine & msg & vbNewLine & _
       "Add these items to task list?", _
       vbYesNo + vbInformation, "Please confirm"#
   End If
    
   If Check = vbYes Then
   
       'This is where I would like to insert the selected items into my cell
       
       Unload Me
   Else
       For i = 0 To ListBox1#ListCount - 1
         ListBox1#Selected#i# = False
       Next
   End If
    
End Sub

I am going to have multiple cells with command buttons that will run this user form.  Each command button will be attached to a certain cell.  I would like this code to return the values to the cells depending on what command button was pushed.  In that way I do not have to write an individual user form to attach to each command button.

I am teaching myself VBA and much of what was above was pieced together from information I have gathered.  I have a very basic knowlege of VBA so any information you could supply would be a huge help.

Thanks

Answer

Hello
In the following links you will find an example


http://programarexcel.blogspot.com/2013/06/listbox-de-excel-de-varias-columnas.h


This code copies the items of a listbox to excel cells
Private Sub CommandButton2_Click ()
Application.ScreenUpdating = True
Dim uf, a As Integer
uf = Sheets ("Sheet2"). Range ("A" & Rows.Count). End (xlUp). Row
uf = uf + 1
a = ListBox1.ListCount

For a = 0 To a - 1
   
  If ListBox1.Selected (a) Then
  Sheets ("Sheet2"). Cells (uf, 1) = ListBox1.List (a, 0)
  Sheets ("Sheet2"). Cells (uf, 2) = ListBox1.List (a, 1)
  Sheets ("Sheet2"). Cells (uf, 3) = ListBox1.List (a, 2)
  Sheets ("Sheet2"). Cells (uf, 4) = ListBox1.List (a, 3)
  Sheets ("Sheet2"). Cells (uf, 5) = ListBox1.List (a, 4)
  uf = uf + 1
  End If
  
Next to
Unload Me
Sheets ("Sheet2"). Select
Range ("A1"). Select
Application.ScreenUpdating = True

The code is PREVIS to for a 5-column listbox but if you have a column must use only

Dim uf, a As Integer
uf = Sheets ("Sheet2"). Range ("A" & Rows.Count). End (xlUp). Row
uf = uf + 1
If ListBox1.Selected (a) Then
Sheets ("Sheet2"). Cells (uf, 1) = ListBox1.List





In http://programarexcel.blogspot.com examples can download free excel macros programmed in VBA, the maros can download and adapt the project that you're doing to automate excel.

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


marcrodos

Expertise

Expert programming macros or visual basic VBA that can help automate your Excel spreadsheets using VBA programming or macros to excel, forms and other procedures related to Macros. I also have an excellent command of Excel in almost its entirety, formulas, graphs, dynamic tables. In http://programarexcel.blogspot.com o www.programarexcel.com examples can download free excel macros programmed in VBA, macros can be downloaded and adapted to the project that you're doing to automate excel www.programarexcel.com

Experience

For several years, use Microsoft Excel on a daily basis, more specifically in my work, which is why over several years I have compiled a wealth of knowledge about their operation, adding to the basic operation of coding excel VBA macros to automate repetitive tasks . I serve as an expert in excel also in www.todoexpertos.com site, which I enjoy a great reputation. In http://programarexcel.blogspot.com o www.programarexcel.com examples can download free excel macros programmed in VBA, macros can be downloaded and adapted to the project that you're doing to automate excel www.programarexcel.com

Organizations
http://www.programarexcel.blogspot.com http://www.programarexcel.com

Publications
In public http://www.programarexcel.com o www.programarexcel.com continuously macro examples that readers can download for free and adapt to the needs of each project in excel that has the web page visitor

Education/Credentials
My profession's is a Chartered Accountant with experience in Excel, especially in VBA or macro programming, but also in the formulas and everything related to excel, I graduated several years ago at the National University of Cordoba, now I practice my profession in which a basic tool is excel. www.programarexcel.com

Awards and Honors
http://www.programarexcel.blogspot.com http://www.programarexcel.com

Past/Present Clients
http://www.programarexcel.blogspot.com http://www.programarexcel.com

©2016 About.com. All rights reserved.