COPYING ONLY A HIGHLIGHTED SELECTION OF USERFORM TEXTBOX TEXT TO A CELL ON ANOTHER SHEET

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
918
Office Version
  1. 365
Platform
  1. Windows
As above. The following code copies the entire Textbox1 container, not just the highlighted selection.
Code:
Private Sub CommandButton1_Click()
    Dim MyData As New DataObject
    MyData.SetText TextBox1.Text  ---> copies entire textbox text - not what I want
    MyData.PutInClipboard
End Sub

Textbox1 text is on Userform1. Code is in a button on Usrform1.
1. The desired text anywhere in Textbox1 is highlighted
2. The button code "reads" and copies only the highlighted text to the clipboard
3. And pastes that highlighted selection in cell A1 of a sheet named VSEDITS
That's it. Images below may help to explain better.

Thanks for anyone's help. My difficulty is in "trapping" only the highlighted selection of any textbox text.
 

Attachments

  • HIGHLIGHTED TEXT TO COPY.jpg
    HIGHLIGHTED TEXT TO COPY.jpg
    93.2 KB · Views: 16
  • HIGHLIGHTED TEXT COPIED TO VSEDITS SHEET.png
    HIGHLIGHTED TEXT COPIED TO VSEDITS SHEET.png
    18.5 KB · Views: 16

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
It is not necessary to put it in memory and then put it in the cell.
You can do it directly:

VBA Code:
Private Sub CommandButton1_Click()
  Sheets("VSEDITS").Range("A1").Value = TextBox1.SelText
End Sub
 
Upvote 0
But if you want it in memory and then to the cell, then:

VBA Code:
Private Sub CommandButton1_Click()
  Dim MyData As New DataObject
  
  MyData.SetText TextBox1.SelText, 1
  MyData.PutInClipboard
  Sheets("VSEDITS").Range("A1").Value = MyData.GetText(1)
End Sub
;)
 
Upvote 0
Hi Dante, first of all thanks for helping. You've helped me greatly in past posts to enhance this application - however in this case, neither of your code does what I want it to - unless i'm missing something. I just copied your code in the button and ran it. It did not paste the highlighted text with changes in cell A1 of sheet VSEDITS. The cell was left blank.

What I want to do is make any changes needed in a specific selected section of a textbox - then highlight that text with
the mouse - grab it, then copy it to a cell on a new sheet. It's exactly the same thing as the copy item on a context menu, except
I want to put that code sequence in a button, so that when clicked, the selection highlighted is automatically copied and pasted into cell AI
of VSEDITS with wany changes made.

Thanks for all your help
cr :)
 
Upvote 0
Hi Dante. Thanks again for your help. I couldn't open your file. The link opened in a Google viewer.
Code:
Private Sub cmdEDITVERSE_Click()
 Sheets("VSEDITS").Range("A1:B1").ClearContents
 Sheets("VSEDITS").Range("A1").Value = NASB.SelText  'this one line of code only worked one time. NASB is just the sheet name
'When I added the line above to clear the contents for new values, nothing pasted into cell A1 of sheet VSEDITS.
'Don't see any reason why not.  

The code below I wrote works great but still copies the entire textbox 'even though I only highlighted the top 4 lines.  It copies the verse
in Textbox8 on the same sheet, along with the value highlighted(supposed to) and just pastes x in cell A1 and the highlighted text in cell
B1 of sheet VSEDITS.

'******************************************works great but copies all text instead of just the highlighted text
   Sheets("VSEDITS").Range("A1:B1").ClearContents
    Dim x As String, ws As Worksheetst
    x = Me.TextBox8.Value
    With BIBLETEXTWINDOW.NASB
       .SelStart = 0
       .SelLength = Len(.Text)
      .Copy
   End With
 cmdEDITVERSE.SetFocus
Set ws = Sheets("VSEDITS")
ws.Cells(1, 1).Value = x
ws.paste Destination:=ws.Range("B1")
     
'******************************************works great but copies all text instead of just the highlighted text
I haven't figured out why this code is not "trapping" on the text highlighted and copying it.
And I don't know why your one line of code in option 1 works for you and not for me every time.

It must be something very simple to fix. This seems way too easy.

Thanks again for all your help.

cr
 
Upvote 0
I couldn't open your file.
You must download the file to your computer.

To pass the selected part to the cell, you must use the SelText property of the textbox.
;)


Try a new userform with a single textbox, select the text and press the button:

1686621430870.png




That's all. You don't need more lines of code.
:cool:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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