Drag & Drop to textboxes

sadsfan

Board Regular
Joined
Apr 30, 2003
Messages
217
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi
Is it possible to drag items from a listbox to a textbox?
If not how do I populate a textbox from the range "B4:B48"? I tried the additem but that didn't work.
Secondly I drag some text into a textbox, but when I drag another item into it it is on the same line without a space. How can I code it so that each new piece of text I drag into a textbox is on a new line?

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Is it possible to drag items from a listbox to a textbox?
Not as far as I'm aware. You could use the Click event if it's not being used for anything else.

If not how do I populate a textbox from the range "B4:B48"?
I think you'd have to loop through the range of cells concatenating the contents of each cell to the textbox. Don't forget to add a linefeed if you want each cell to appear on a separate line in the textbox.

Secondly I drag some text into a textbox, but when I drag another item into it it is on the same line without a space. How can I code it so that each new piece of text I drag into a textbox is on a new line?
Set the MultiLine property of the text box to True and use the BeforeDropOrPaste event handler to do this:-
Code:
TextBox1 = TextBox1 & vbLf
You have to be careful to drop the new text right at the end of the text box though.
 
Upvote 0
You can use the MouseMove event of the listbox:
Code:
Private Sub ListBox1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)

Dim MyDataObject As DataObject
If Button = 1 Then
Set MyDataObject = New DataObject
Dim Effect As Integer
MyDataObject.SetText ListBox1.Value
Effect = MyDataObject.StartDrag
End If
End Sub

Private Sub TextBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
   Cancel = True
   Effect = 1
   TextBox1.Text = TextBox1.Text & vbNewLine & Data.GetText
End Sub

for example.
 
Upvote 0
Thanks Guys, both of those replies worked a treat. Ruddles, is there any way to stop the first piece of text being dropped in leaving a blank line?
 
Upvote 0
Thanks Guys, both of those replies worked a treat. Ruddles, is there any way to stop the first piece of text being dropped in leaving a blank line?
Check if the text box is empty: if it is, don't do anything; if it isn't, add the vbLf.

Rorya's solution looks better than mine though.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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