pass userform subform listbox item to main form

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I have a userform which contains a subform with a couple of listboxes in it. The objective is that the user select certain items from listbox 1, to pass to listbox 2, and when completed to click a submit button, and all of those items go to the main form. I can't put the listboxes opn the main form, because the form is already too busy (not enough real estate, as it were).
I scrounged the code to select each item individually in the second listbox using this code:

Code:
Private Sub cmdSubmitRep_Click()
    For i = 0 To listbox2.ListCount - 1
        listbox2.Selected(i) = True
    Next i
    
End Sub

After the item is selected I want it transferred to Userform1.txtRep1. I can't seem to get that working. Additionally, what I'd like is that if there is more than one item selected, to have the items separated with either a dash or a comma (no real preference really).

any help is appreciated.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I think what you want is:

Code:
Private Sub cmdSubmitRep_Click()
    Dim sSel, iCt
    UserForm1.txtRep1 = ""
    With Me.ListBox2
        If .ListCount < 1 Then Exit Sub
        ReDim sSel(.ListCount - 1)
        For iCt = 0 To .ListCount - 1
            sSel(iCt) = .List(iCt, 0)
        Next iCt
    End With
    UserForm1.txtRep1 = Join(sSel, ", ")
End Sub
 
Upvote 0
I'm not sure what you mean by "main form" and "subform"

Are you talking about a user form that invokes a different user form? Or are you talking about a multipage control on a single user form? Or something else
 
Upvote 0
Hi All and thanks for your answers. tlowry, thanks, the code is working fine.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,696
Members
449,464
Latest member
againofsoul

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