VBA Listbox for each selection (Multiselect) value

johnpiper

New Member
Joined
Jan 28, 2022
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hi I need a VBA script which will run through each selected value in a Listbox and print the value into a cell.

So selected are Answer 1, Ans 4, Ans 6, Ans 7, Ans9, Ans 10...

It'll, in turn, change the value of A1 to be "Answer 1", then replace that cell with the next value, "Ans 4", then the next value selected, "Ans 6"... And so on until all selections have been printed and no more remain. It should skip any values from the ListBox not highlighted.

Thanks in advance!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Thanks for posting on the forum.

Try this on a commandbutton.
Fit the name "Sheet1" to the name of your sheet:
VBA Code:
Private Sub CommandButton1_Click()
  Dim i As Long
 
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) = True Then
        Sheets("Sheet1").Range("A1").Value = .List(i)
      End If
    Next
  End With
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 1
Solution
Code:
Private Sub CommandButton1_Click()
Dim i As Integer

Range("A1").Select
For i = 0 To ListBox1.ListCount - 1
  If ListBox1.Selected(i) Then
     ActiveCell.Value = ListBox1.List(i)
    ActiveCell.Offset(1, 0).Select  'next cell
  endif
Next
End Sub
 
Upvote 1
Thanks for posting on the forum.

Try this on a commandbutton.
Fit the name "Sheet1" to the name of your sheet:
VBA Code:
Private Sub CommandButton1_Click()
  Dim i As Long
 
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) = True Then
        Sheets("Sheet1").Range("A1").Value = .List(i)
      End If
    Next
  End With
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Perfect, thanks!
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top