Listbox userform, multiple values into same cell.

freddie mitchell

New Member
Joined
Apr 14, 2011
Messages
43
Hello lovely people,

With great help from hero member Fluff, I've got a code that adds values entered into a listbox by a user. This code is perfect at putting multiple values into multiple cells. For example, value X, Y, Z go into cells A1, A2 and A3.

However, can anyone help me change this code so that multiple values can be put into one cell. For example, value X, Y, Z all go into cell A1.
Below is my abortive attempt to modify Fluff's code for entering values into one cell. As it stands, if the user selects either "peanut" or "hazelnut" then they will appear in cell 4. But if the user selects both peanut and hazelnut then only hazelnut will appear in the cell.


VBA Code:
Dim h As Long
With Me.allergylist
    For h = 0 To .ListCount - 1
    If .Selected(h) Then
    Select Case .List(h)
                Case "peanuts"
                Cells(r, 4).Value = Cells(r, 5).Value & "peanuts"
                Case "hazelnuts"
                Cells(r, 4).Value = Cells(r, 5).Value & "hazelnuts"
               
End Select
End If
Next h
End With
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,772
Office Version
  1. 2007
Platform
  1. Windows
Try this:

VBA Code:
  Dim h As Long
  With Me.allergylist
    For h = 0 To .ListCount - 1
      If .Selected(h) Then
        Select Case .List(h)
          Case "peanuts", "hazelnuts"
            Cells(r, 4).Value = Cells(r, 4).Value & .List(h) & " "
        End Select
      End If
    Next h
  End With
 
Solution

freddie mitchell

New Member
Joined
Apr 14, 2011
Messages
43
Thank you, its just the ticket! Out of curiosity, why did the original modified code not work? Was it overwriting each entry?
 

Forum statistics

Threads
1,144,580
Messages
5,725,099
Members
422,590
Latest member
Mikeyyy

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
Top