72

I'm trying to add a button to a spreadsheet that when clicked will copy a specific URL to my clipboard.

I had a bit of knowledge of Excel VBA but it's been a while and I'm struggling.

2
  • 2
    Welcome to stackoverflow! If you could share what you've tried so far you're more likely to receive assistance solving your problem. Commented Jan 8, 2013 at 16:39
  • 1
    Windows 10 x64 & Office 2016 x64: stackoverflow.com/a/42514269/2504779 Commented Feb 28, 2017 at 16:46

12 Answers 12

116

EDIT - MSForms is deprecated, so you should no longer use my answer. Instead use this answer: https://stackoverflow.com/a/60896244/692098

I leave my original answer here only for reference:

Sub CopyText(Text As String)
    'VBA Macro using late binding to copy text to clipboard.
    'By Justin Kay, 8/15/2014
    Dim MSForms_DataObject As Object
    Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    MSForms_DataObject.SetText Text
    MSForms_DataObject.PutInClipboard
    Set MSForms_DataObject = Nothing
End Sub

Usage:

Sub CopySelection()
    CopyText Selection.Text
End Sub
Sign up to request clarification or add additional context in comments.

9 Comments

great help and altho relying on magic strings, neatly saves an aditional reference to 3rd party dll's etc. plus 1
This code has a bug, eventually it will stop copying your text and copy only 2 question marks.
@gordon613, I get that it's a Microsoft bug. It was 5 years ago that I put this post together, so I'm not going to get too defensive of it. I am curious though. Under what circumstances does it fail? Are you writing a repeating copy/paste routine that fails after a certain number, lets say 250 iterations? Does it reset by just restarting the Excel or whatever VBA provider you're using? Do you have to reboot windows to reset it? Or does it just happen intermittently and randomly?
@Jroonk this method fails (at least in Windows 10, not sure about other Windows versions) if File Explorer is open to a folder. Depending on the encoding of the paste destination it may paste as ?? or \xEF\xBF\xBF\xEF\xBF\xBF.
@SendETHToThisAddress, really should use this other answer now for copying to clipboard in VBA: stackoverflow.com/a/60896244/692098 He's doing the same thing, but he's using htmlfile object to access the clipboard, instead of the deprecated MSForms object.
|
62

To write text to (or read text from) the Windows clipboard use this VBA function:

Function Clipboard$(Optional s$)
    Dim v: v = s  'Cast to variant for 64-bit VBA support
    With CreateObject("htmlfile")
    With .parentWindow.clipboardData
        Select Case True
            Case Len(s): .setData "text", v
            Case Else:   Clipboard = .getData("text")
        End Select
    End With
    End With
End Function

'Three examples of copying text to the clipboard:
Clipboard "Excel Hero was here."
Clipboard var1 & vbLF & var2
Clipboard 123

'To read text from the clipboard:
MsgBox Clipboard

This is a solution that does NOT use MS Forms nor the Win32 API. Instead it uses the Microsoft HTML Object Library which is fast and ubiquitous and NOT deprecated by Microsoft like MS Forms. And this solution respects line feeds. This solution also works from 64-bit Office. Finally, this solution allows both writing to and reading from the Windows clipboard. No other solution on this page has these benefits.

36 Comments

Solution to Invalid Argument problem (works in 64-bit Excel 2010): The second argument to .setData has to be a Variant (a string literal like "test" also works), not a variable of type String. Create a Variant variable, assign s to it, and use it instead of s in the .setData and it works fine.
@Shodan. The $ means STRING.
@Shodan It's exactly the same as: Function Clipboard() As String
@Shodan That notation has been native to BASIC since before VBA or even Visual Basic existed.
@Timo Indeed and that is why I described this solution as writing and reading TEXT.
|
22

The simplest (Non Win32) way is to add a UserForm to your VBA project (if you don't already have one) or alternatively add a reference to Microsoft Forms 2 Object Library, then from a sheet/module you can simply:

With New MSForms.DataObject
    .SetText "http://zombo.com"
    .PutInClipboard
End With

3 Comments

This is the method I use, but I found it fails if a folder is open in File Explorer in Windows 10. I can't comment on other Windows versions.
Wow - great find @ChrisB - I have been trying to troubleshoot that issue for years!
MSForms is deprecated, so you should no longer it. Instead use this answer: stackoverflow.com/a/60896244/692098
12

If the url is in a cell in your workbook, you can simply copy the value from that cell:

Private Sub CommandButton1_Click()
    Sheets("Sheet1").Range("A1").Copy
End Sub

(Add a button by using the developer tab. Customize the ribbon if it isn't visible.)

If the url isn't in the workbook, you can use the Windows API. The code that follows can be found here: http://support.microsoft.com/kb/210216

After you've added the API calls below, change the code behind the button to copy to the clipboard:

Private Sub CommandButton1_Click()
    ClipBoard_SetData ("http:\\stackoverflow.com")
End Sub

Add a new module to your workbook and paste in the following code:

Option Explicit

Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
   As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
   As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
   ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
   As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
   ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
   As Long, ByVal hMem As Long) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Function ClipBoard_SetData(MyString As String)
   Dim hGlobalMemory As Long, lpGlobalMemory As Long
   Dim hClipMemory As Long, X As Long

   ' Allocate moveable global memory.
   '-------------------------------------------
   hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

   ' Lock the block to get a far pointer
   ' to this memory.
   lpGlobalMemory = GlobalLock(hGlobalMemory)

   ' Copy the string to this global memory.
   lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

   ' Unlock the memory.
   If GlobalUnlock(hGlobalMemory) <> 0 Then
      MsgBox "Could not unlock memory location. Copy aborted."
      GoTo OutOfHere2
   End If

   ' Open the Clipboard to copy data to.
   If OpenClipboard(0&) = 0 Then
      MsgBox "Could not open the Clipboard. Copy aborted."
      Exit Function
   End If

   ' Clear the Clipboard.
   X = EmptyClipboard()

   ' Copy the data to the Clipboard.
   hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

OutOfHere2:

   If CloseClipboard() = 0 Then
      MsgBox "Could not close Clipboard."
   End If

End Function

5 Comments

From the KB, this code targets Access 2000. The code was probably meant to work on older OS that did not support Unicode properly (Win95 anyone?). Unfortunately, we keep using the same piece of code that won't work if your string contains any characters outside the extended ASCII range, so no Unicode for you!
Although it doesn't answer the OP's question on getting a URL to the clipboard that may or may not be in a cell, +1 for the .Copy method reference. Duh, sometimes the simplest answers are the best!
This is my new preferred method! It doesn't fail when File Explorer is open to a folder.
The first approach (Range.Copy) won't copy text but cells: Excel will add quotation marks (") at the start and the end of the text and replace quotation marks by double-quotation-marks
10

Add a reference to the Microsoft Forms 2.0 Object Library and try this code. It only works with text, not with other data types.

Dim DataObj As New MSForms.DataObject

'Put a string in the clipboard
DataObj.SetText "Hello!"
DataObj.PutInClipboard

'Get a string from the clipboard
DataObj.GetFromClipboard
Debug.Print DataObj.GetText

Here you can find more details about how to use the clipboard with VBA.

3 Comments

This code has a bug, eventually it will stop copying your text and copy only 2 question marks
MSForms is deprecated, so you should no longer use it. Instead use this answer: stackoverflow.com/a/60896244/692098
"MSForms is deprecated" since when? I agree hat the DataObject isn't very stable but the linked method is Windows only. Is there a method for VBA on both platforms?
5

If you want to put a variable's value in the clipboard using the Immediate window, you can use this single line to easily put a breakpoint in your code:

Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}"): MSForms_DataObject.SetText VARIABLENAME: MSForms_DataObject.PutInClipboard: Set MSForms_DataObject = Nothing

1 Comment

This code has a bug, eventually it will stop copying your text and copy only 2 question marks
4

If you just want to copy to clipboard, you can summarize the answer https://stackoverflow.com/a/60896244/692098 like this:

Sub copyClip(yourtext As String)
   Dim v
   v = yourtext 'This must be a variant to work with 32 and 64 bit versions of Excel
   CreateObject("htmlfile").parentWindow.clipboardData.setData "text", v
End Sub

1 Comment

OPINION: I appreciate what you've done. However, that line is still complicated enough that its worth putting into a function. Not good practice to copy/paste that everywhere. FYI: Also based on the linked answer, to READ text from clipboard could have this simple function (not tested): Function GetClipboard$() Clipboard = CreateObject("htmlfile").parentWindow.clipboardData.getData("text") End Function. Usage: Dim str As String: str = GetClipboard. [This might be easier for a novice vba coder to understand, than the original, which combined read and write into a single function.]
2

Simple method utilizing cmd shell in Windows.

Sub PutToClip()
    
    Dim txt As String: txt = "This works!"
    Call Shell("cmd.exe /k" & "echo|set /p=" & txt & "| clip", vbHide)
    
End Sub

1 Comment

This seems to work the best for me, but there's a CRLF added into the copied string. Is it possible to get rid of that?
1

If you want a one-liner, no extra Sub, Function or Dim, this is it (replace S with your string or variable):

CreateObject("htmlfile").ParentWindow.ClipboardData.SetData "text", CVar(S)

Comments

0

If the place you're gonna paste have no problem with pasting a table formating (like the browser URL bar), I think the easiest way is this:

Sheets(1).Range("A1000").Value = string
Sheets(1).Range("A1000").Copy
MsgBox "Paste before closing this dialog."
Sheets(1).Range("A1000").Value = ""

1 Comment

Note that that "dirties" the workbook. Another problem is that if there's cell or sheet protection, you may have to cumbersomely deal with that. An alternative without those issues is workbooks.add, cells(1)=string, cells(1).copy, and close with SaveChanges:=False
0

The code given at the Microsoft site works in Excel too, even though it is under Access VBA. I tried it in Excel 365 on a 64 bit Windows 10.

Microsoft Site Link: https://learn.microsoft.com/en-us/office/vba/access/Concepts/Windows-API/send-information-to-the-clipboard

Copying here for answer completeness.

Option Explicit
Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare Function CloseClipboard Lib "user32.dll" () As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32.dll" (ByVal wFormat As Long) As Long
Private Declare Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Long, ByVal lpString2 As Long) As Long

Public Sub SetClipboard(sUniText As String)
    Dim iStrPtr As Long
    Dim iLen As Long
    Dim iLock As Long
    Const GMEM_MOVEABLE As Long = &H2
    Const GMEM_ZEROINIT As Long = &H40
    Const CF_UNICODETEXT As Long = &HD
    OpenClipboard 0&
    EmptyClipboard
    iLen = LenB(sUniText) + 2&
    iStrPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, iLen)
    iLock = GlobalLock(iStrPtr)
    lstrcpy iLock, StrPtr(sUniText)
    GlobalUnlock iStrPtr
    SetClipboardData CF_UNICODETEXT, iStrPtr
    CloseClipboard
End Sub

Public Function GetClipboard() As String
    Dim iStrPtr As Long
    Dim iLen As Long
    Dim iLock As Long
    Dim sUniText As String
    Const CF_UNICODETEXT As Long = 13&
    OpenClipboard 0&
    If IsClipboardFormatAvailable(CF_UNICODETEXT) Then
        iStrPtr = GetClipboardData(CF_UNICODETEXT)
        If iStrPtr Then
            iLock = GlobalLock(iStrPtr)
            iLen = GlobalSize(iStrPtr)
            sUniText = String$(iLen \ 2& - 1&, vbNullChar)
            lstrcpy StrPtr(sUniText), iLock
            GlobalUnlock iStrPtr
        End If
        GetClipboard = sUniText
    End If
    CloseClipboard
End Function

The above code can be called from a Custom Macro as follows:

Sub TestClipboard()
    Dim Val1 As String: Val1 = "Hello Clipboard " & vbLf & "World!"
    SetClipboard Val1
    MsgBox GetClipboard
End Sub

To Show a button on a form, you can find a good example by a quick serach. To show a button in the Excel Custom Ribbon (One that shows only in the current Excel workbook) you can use CustomUI.

CustomUI links:

https://bettersolutions.com/vba/ribbon/custom-ui-editor.htm

https://learn.microsoft.com/en-us/office/open-xml/how-to-add-custom-ui-to-a-spreadsheet-document

imageMSO List with Icons (Used in CustomUI):

https://bert-toolkit.com/imagemso-list.html

Thanks.

Comments

-1

I tested this code in excel 365 and it worked

Dim str as String
str = "Hello Copied"
Windows.Parent.Clipboard str

Note: I created the variable because the code does not process string concatenation

1 Comment

This did not work for me. I get an error "Wrong number of arguments or invalid property assignment"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.