View Single Post
 
Old 07-22-2020, 09:07 AM
gmaxey gmaxey is offline Windows 10 Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

When you post code that someone else provided to you, you should at least acknowledge the source!


Have to use a ListBox???? oListOrComboBox as object should make it pretty clear that you can use a List or ComboBox!!!


Regardless, writing the value selected in a listbox to a textbox has little to do with how that listbox was populated.


Code:
Private Sub UserForm_Initialize()
  With ListBox1
    .AddItem "A"
    .AddItem "B"
    .AddItem "C"
  End With
  With ListBox2
    .AddItem
    .List(.ListCount - 1, 0) = "A"
    .List(.ListCount - 1, 1) = "Apples"
    .AddItem
    .List(.ListCount - 1, 0) = "B"
    .List(.ListCount - 1, 1) = "Birds"
    .AddItem
    .List(.ListCount - 1, 0) = "C"
    .List(.ListCount - 1, 1) = "Cats"
  End With
End Sub

Private Sub CommandButton1_Click()
  TextBox1 = vbNullString
  TextBox2 = vbNullString
  If ListBox1.ListIndex <> -1 Then TextBox1 = ListBox1
  If ListBox2.ListIndex <> -1 Then TextBox2 = ListBox2 'Uses the bound column
  'Or use an explicit indexed column
  'If ListBox2.ListIndex <> -1 Then TextBox2 = ListBox2.Column(0)
  'If ListBox2.ListIndex <> -1 Then TextBox2 = ListBox2.Column(1)
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote