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

jptaz

New Member
Joined
May 1, 2020
Messages
46
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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Maybe this:
VBA Code:
Private Sub Listbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
activeTxTbox = Trim(activeTxTbox & " " & ListBox1.Value)
End Sub
 
Upvote 0
Solution
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 :)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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