Copy Command Button caption to Clipboard

chester1993

New Member
Joined
Jan 26, 2016
Messages
40
Hello everyone.

As the title say, I have multiple (at least 10 I think) cmdbuttons on a userform. I have 2 things I am looking to achieve:

1. Copy the cmdbutton captions to clipboard when clicked. This will be pasted on another program like Notepad, etc.
2. Show the cmdbutton captions on Textbox1 when clicked.

Not sure if there's a way to do both in 1-click. If there is, that would be wonderful. If not, I'd choose what I want to do once I see the codes.

Thanks in advance!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
One way
VBA Code:
Private Sub CommandButton1_Click()
'put string in clipboard and in textbox1
    Dim Obj As New MSForms.DataObject, t As String
    t = Me.CommandButton1.Caption
    Obj.SetText t
    Obj.PutInClipboard
    Me.TextBox1 = t
End Sub

Another
VBA Code:
Private Sub CommandButton1_Click()
'put string in clipboard
    Dim Obj As New MSForms.DataObject
    Obj.SetText Me.CommandButton1.Caption
    Obj.PutInClipboard
    
'place clipboard content in textbox1
    Obj.GetFromClipboard
    Me.TextBox1 = Obj.GetText
End Sub
 
Upvote 0
Thanks for your help, @Yongle. The code work wonders! Just a side note, what if I want to add a message box saying "Copied" once the buttons are clicked? More like a fading message box. How would I go about that along with these initial codes?

Edit: I tried searching for the answer, looks like it requires intensive coding. Not sure if its still the same at this point. If it is, then don't mind my request. LOL.
 
Upvote 0
There are several ways to metaphorically skin a cat and with a bit of imagination you may be able to achieve something very similar with very basic coding

I need to understand what else is going on so that I can suggest a method

Is the command button caption ALWAYS the same text or does it change ?
- if the caption changes, what triggers that change?

What does the user do immediately after clicking on the command button ?

Why do you not want to use a standard message box and keep life simple ?
 
Upvote 0
The command button caption does not change. Technically, its a button that would copy the caption of that button. As I mentioned above, it will be pasted on another program like Notepad, etc.

Ideally, what I want additionally is to have a "notification" that the caption was copied. Like a small rectangular message box saying "copied" once the button is clicked.
 
Upvote 0
Also, currently I am using a messagebox to make it simplier as you suggested. This is only to enhance my knowledge on Excel VBA. I just thought the message box is too 'big' for visual purposes.
 
Upvote 0
You could simply use the Command Button caption itself - so that it changes to "Copied" after clicking on it
The mousemove event on a transparent active-x label (bigger and behind the first button) triggers the text to revert to the required caption

METHOD
Insert active-x label and make it taller and wider than the command button
Place it over the command button
Move it behind the command button (right-click \ Order\Send to Back)
Line it up so that it overlaps in each direction
Delete label caption
Right-click \properties \set back style to transparent and border to none
Group the 2 objects together (select both\ right-click\Group)

Add this as the last line of the original macro I gave you, so that the command button caption changes after copying
VBA Code:
Me.CommandButton1.Caption = "Copied"

Add this code
VBA Code:
Private Sub Label1_Click()
    CommandButton1.Activate
End Sub
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    CommandButton1.Caption = "This is the text I want"
End Sub

Label properties.jpg
 
Upvote 0
You could make it even simpler on a userform and NOT use a label but instead make use of UserForm_MouseMove
- but I was concerned that your userform has many buttons and I did not want you to have a different problem
- you know what is happening in your userform and could perhaps make it work (the technique is identical)

This is the toggle
VBA Code:
Private Sub CommandButton1_Click()
    CommandButton1.Caption = "Copied"
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    CommandButton1.Caption = "This is the text I want"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
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