Copy textbox contents with Excel VBA?

  Simsy 12:41 16 Feb 2005

Hi folks,

I've simplified this problem to the basics.

I have a user form in Excel, (2000). It displays some text in a textbox

I want to copy the contents, (to the Windows clipboard), of the textbox using a button.

The textbox is called txtTIP and I have the following code with the Click event of the button;


This doesn't seem to work, unless I have previously selected the contents of the textbox. I can't seem to work out how to do this in VBA.

Can anyone advise?

Thanks in advance.



  VoG II 12:44 16 Feb 2005



should work.

  Simsy 12:58 16 Feb 2005

tried that VoG™, and get the error message;

Compile error.
Argument not optional.

and it the .Select that appears highlighted.

There is a .selText option that appears from the dropdown after typing the "." but that results in the error message

Compile error.
Invalid use of property.

I'm using Excel2000 on win98se, in case that's relevant?

Any more suggestions?




  VoG II 13:02 16 Feb 2005



  VoG II 13:03 16 Feb 2005

Or if txtTIP is not declared as a variable


  Simsy 13:11 16 Feb 2005

but that's not working either, I think becasue the textbox isn't on a sheet, but on a form.

I need to log off now, bu ti'd welcome any more suggestions!



  VoG II 13:30 16 Feb 2005

I can't get the Copy to work either. However this works if a little untidy:

Sub tst2()

Dim txt As String

txt = UserForm1.TextBox1.Value

Range("A1").Value = txt


End Sub

The text in A1 will disappear when the text is pasted elsewhere.

  rogertjj 14:56 16 Feb 2005

If you type :

Application.CutCopyMode = xlCopy

before you try and copy the text, it should work

  rogertjj 14:58 16 Feb 2005

Sorry, ignore me. This only works if the text in the textbox is already selected !!

  rogertjj 15:10 16 Feb 2005

click here may help ?

I found the following code in MrExcel posted by DK and I quote:

"You can use the DataObject provided by the MS Forms library. For example, say you have a textbox called TextBox1 and a command button named cmdCopy this code will copy the contents of the textbox to the clipboard."


Private Sub cmdCopy_Click()

Dim YourDataObject As DataObject

If Me.TextBox1.Text <> "" Then

Set YourDataObject = New DataObject

YourDataObject.SetText Me.TextBox1.Text


End If

End Sub


Yet another element to add to my growing list of things to learn. Once the form is hidden then you can paste the result manually wherever you wish.

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

Alienware 17 R4 2017 review

These brilliant Lego posters show just what children's imaginations are capable of

Mac power user tips and hidden tricks

Comment réinitialiser votre PC, ordinateur portable ou tablette Windows ?