populating a textbox on a userform from selections that are made from a listbox that is one another userform (popup)...

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
here is what I have right now and what I am wanting the code to do...

I have a list box that is shown (on a separate popup form) and it gets populated from a table once this command button is clicked:
Capture46.JPG


after being clicked, the popup form is shown and the listbox gets populated from data on a worksheet. ("Sheet6" , "Table1" )
Capture48.JPG

then the user makes whatever selections from the listbox:
Capture44.JPG


and that's the extent of what I have...

from here, what I would like the code to do is to populate a text box back that is back on the main userform from the choices that were just selected from the listbox.

Preferably, the selected choices would be shown like this where they are separated by a comma:
Capture47.JPG


my Code right now only does this:

After clicking on the command button in the first pic, the popup form is shown:
VBA Code:
Private Sub cmdShowCustomers_Click()
    frmShowCustomers.Show
End Sub
And on the popup form the listbox is populated from a table on a worksheet:
VBA Code:
Private Sub UserForm_Initialize()
'
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")
'******************
Dim xCus As Range
''*******************************************************************************************************
lstCustomer.List = Sheet6.ListObjects("Table1").DataBodyRange.Value2
lstCustomer.ColumnCount = 2
'
End Sub

Getting the choices that were made back onto the main userform (name: frmReportCriteria), and then to populate the textbox (name: txtCustomer),,, I cant figure out.

Any help or other suggestions (if what I am explaining cant be done), would be appreciated. Thank you.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Use this function within your UserForm code module:
VBA Code:
Private Function getSelection(lbx As MSForms.ListBox) As String
    Dim sel As String
   
    With lbx
        Dim i As Long: For i = 0 To .ListCount - 1
            If .Selected(i) Then
                sel = sel & .List(i) & ","
            End If
        Next i
    End With
   
    If Len(sel) > 0 Then
        getSelection = Left(sel, Len(sel) - 1)
    End If
End Function
If you're going to use it somewhere else, put it in a module and change from Private to Public.

You can use it like this:
VBA Code:
Private Sub lstCustomer_Change()
    Me.txtCustomer.Value = getSelection(Me.lstCustomer)
End Sub
With this, every time a selection is made, the contents of your textbox will be overwritten with the selected values.
 
Upvote 0
Solution
Use this function within your UserForm code module:
VBA Code:
Private Function getSelection(lbx As MSForms.ListBox) As String
    Dim sel As String
 
    With lbx
        Dim i As Long: For i = 0 To .ListCount - 1
            If .Selected(i) Then
                sel = sel & .List(i) & ","
            End If
        Next i
    End With
 
    If Len(sel) > 0 Then
        getSelection = Left(sel, Len(sel) - 1)
    End If
End Function
If you're going to use it somewhere else, put it in a module and change from Private to Public.

You can use it like this:
VBA Code:
Private Sub lstCustomer_Change()
    Me.txtCustomer.Value = getSelection(Me.lstCustomer)
End Sub
With this, every time a selection is made, the contents of your textbox will be overwritten with the selected values.

Excellent! Thank you. (y)


Just one issue with this;

After the 'popup' userform ("frmShowCustomers") is closed out (which I have circled in blue), I need what is shown in the textbox "txtCustomerX" to get moved over to the textbox titled "txtCustomer" on the userform "frmGetReportCriteria" (which is the main userform.).
Capture55.JPG

Here is the code after revising it with what you gave me:
(this is the code in its entirety that I have listed for the popup userform... "frmShowCustomers")

VBA Code:
Private Sub UserForm_Initialize()
'
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")
'******************
Dim xCus As Range
''*****************
lstCustomer.List = Sheet6.ListObjects("Table1").DataBodyRange.Value2
lstCustomer.ColumnCount = 2
'
End Sub
'******************
Private Function getSelection(lbx As MSForms.ListBox) As String
    Dim sel As String
 
    With lbx
        Dim i As Long: For i = 0 To .ListCount - 1
            If .Selected(i) Then
                sel = sel & .List(i) & ", "
            End If
        Next i
    End With
 
    If Len(sel) > 0 Then
        getSelection = Left(sel, Len(sel) - 1)
    End If
End Function
'******************
'
'
'
'
'******************
Private Sub lstCustomer_Change()
    Me.txtCustomerX.value = getSelection(Me.lstCustomer)
End Sub
'******************
Private Sub frmShowCustomers_terminate()
    frmReportCriteria.txtCustomer.value = getSelection(Me.lstCustomer)
    Unload Me
End Sub
'******************


That last part of the code ("Private Sub frmShowCustomers_terminate()") is where I was attempting to get what is listed in the textbox "txtCustomerX" copied over to the main userform and to become the value for "txtCustomer" on the userform "frmReportCriteria"... what am I doing wrong here? Thanks again.
 
Upvote 0
Do you get any error?

Are you sure those are the names? Post a screenshot of your project explorer, it should look somewhat like this, but with your names:
1710799836505.png
 
Upvote 0
Do you get any error?

Are you sure those are the names? Post a screenshot of your project explorer, it should look somewhat like this, but with your names:
View attachment 108581


Yes. I did have something wrong.

I just realized it what I thought I might of done thinking about it on my way home from work, and as soon as I got home I checked it, and sure enough, I had to wrong!!


Capture59.JPG

I had a 'Me.' in front of the name of the USERFORM name. 🤦‍♂️

As soon as I fixed that, it now populates both userforms in real time each time that a change is made. 💡👌
Capture57.JPG

Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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