adding data to a textbox from a listbox without replacing or erasing text

jptaz

New Member
Joined
May 1, 2020
Messages
42
Office Version
  1. 2010
Platform
  1. Windows
Hello,
I'm using this code to copy listbox item to a textbox, however, I would like add the item without replacing everything in the textbox. Example : I write in the textbox "the apple is" and when I click the red item in the listbox, it completes the sentence. Is it possible?

thank you for your time



VBA Code:
Option Explicit
Public WithEvents TbTX As MSForms.TextBox
Dim cls() As New UserForm1
Public activeTxTbox As Object

Private Sub Listbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
activeTxTbox = ListBox1.Value 
End Sub

Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
With activeTxTbox: .SetFocus: .SelStart = Len(.Value): End With 

End Sub

Private Sub TbTX_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Set UserForm1.activeTxTbox = TbTX
End Sub

Private Sub TbTX_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
End Sub

Private Sub UserForm_Activate()
Dim ctrl, a&
For Each ctrl In Me.Frame1.Controls
If TypeName(ctrl) = "TextBox" Then a = a + 1: ReDim Preserve cls(1 To a): Set cls(a).TbTX = ctrl
Next
End Sub

Private Sub UserForm_Initialize()
      ListBox1.List = Array("red", "blue", "green", "black", "yellow")
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
3,648
Office Version
  1. 365
Platform
  1. Windows
Maybe this:
VBA Code:
Private Sub Listbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
activeTxTbox = Trim(activeTxTbox & " " & ListBox1.Value)
End Sub
 
Solution

jptaz

New Member
Joined
May 1, 2020
Messages
42
Office Version
  1. 2010
Platform
  1. Windows
Maybe this:
VBA Code:
Private Sub Listbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
activeTxTbox = Trim(activeTxTbox & " " & ListBox1.Value)
End Sub
Thank you it works like a charm!

Have a nice day :)
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
3,648
Office Version
  1. 365
Platform
  1. Windows
You're welcome, glad to help & thanks for the feedback.:)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,042
Messages
5,767,793
Members
425,435
Latest member
cmardaz

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