Paste text to textbox via button VBA Excel

2.5k Views Asked by At

I want a copy and a paste button in MS Excel, the copy button looks like this

Private Sub CommandButton1_Click()
Dim MyData As New DataObject
MyData.SetText TextBox1.Text
MyData.PutInClipboard
End Sub

Now, how can I make/code a PASTE button in a similar fashion?

1

There are 1 best solutions below

1
On

As mentioned in the comments, the post from Get text from clipboard using GetText - avoid error on empty clipboard helped me arrive at the solution that I was looking for.

    Dim DataObj As MsForms.DataObject
    Set DataObj = New MsForms.DataObject 

    On Error GoTo Whoa

    '~~> Get data from the clipboard.
    DataObj.GetFromClipboard

    '~~> Get clipboard contents
    Me.txtKordinatat.Value = DataObj.GetText(1)

    Exit Sub
Whoa:
    If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"