I can do this easily enough by writing the variable to a cell and then using Range.copy (and then erasing the cell contents), but I am surprised there doesn’t seem to be a simple direct way of doing this.
Or am I wrong ?
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Copy VBA variable’s numeric value to clipboard
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Copy VBA variable’s numeric value to clipboard
- This topic has 12 replies, 6 voices, and was last updated 9 years, 4 months ago.
AuthorTopicViewing 10 reply threadsAuthorReplies-
RetiredGeek
AskWoody_MVPDecember 18, 2015 at 10:09 am #1542730Martin,
Googling: vba copy to clipboard
returns quite a few items including one using API calls.HTH :cheers:
-
Maudibe
AskWoody_MVPDecember 18, 2015 at 3:47 pm #1542783Martin,
If RG’s link doesn’t provide what you need, the following code works. Run the SAMPLECALL routine to call the function that loads the passed variable to the clipboard. Change the line a=”test” to the value you want. After the code is run, Ctrl-V will paste the variables value.
HTH,
MaudCode: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 Public Sub SAMPLECALL() Dim a As Variant a = “test” VarCopy a End Sub Public Function VarCopy(Variable As Variant) Dim MemLoc1 As Long, MemLoc2 As Long Dim MemLoc3 As Long, X As Long Dim strng As String, MyString As String MyString = Variable MemLoc1 = GlobalAlloc(GHND, Len(MyString) + 1) MemLoc2 = GlobalLock(MemLoc1) MemLoc2 = lstrcpy(MemLoc2, MyString) If GlobalUnlock(MemLoc1) 0 Then MsgBox “Could not unlock memory location. Copy aborted.” GoTo Continue End If If OpenClipboard(0&) = 0 Then MsgBox “Could not open the Clipboard. Copy aborted.” Exit Function End If X = EmptyClipboard() MemLoc3 = SetClipboardData(CF_TEXT, MemLoc1) Continue: If CloseClipboard() = 0 Then MsgBox “Could not close Clipboard.” End If End Function
-
WSMartinM
AskWoody LoungerDecember 18, 2015 at 4:30 pm #1542808 -
WSMartinM
AskWoody LoungerDecember 18, 2015 at 4:54 pm #1542809Inspired by RG and Maud, I think this is as simple as it gets:
Code:Dim DataObj As New MSForms.DataObject Dim Number As String Number = [VBA variable or Expression] DataObj.SetText Number DataObj.PutInClipboard
It hadn’t really occurred to me that the Clipboard only contains strings rather than numbers.
For this code to work you have to reference the Microsoft Forms 2.0 Object Library [VBA . . .Tools . . . References]
-
RetiredGeek
AskWoody_MVPDecember 18, 2015 at 5:18 pm #1542810Martin,
Nice Job.
Here’s a function with some improvements.
1. Doesn’t need reference due to late binding
2. Use of Variant as argument type allows both numbers (yes when pasted they can be operated on) and strings.Code:Option Explicit Function WriteToClipBoard(vValue As Variant) Dim DataObj As Object '*** Using Late Binding to MSForms.DataObject Set DataObj = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") With DataObj .SetText vValue .PutInClipboard End With End Function 'WriteToClipBoard Sub Test() WriteToClipBoard "Hello" 'Also test just 5 and it came in as number! End Sub
HTH :cheers:
-
WSMartinM
AskWoody Lounger -
WSrory
AskWoody LoungerDecember 21, 2015 at 4:02 am #1542992 -
WSMartinM
AskWoody Lounger -
zeddy
AskWoody_MVPDecember 23, 2015 at 2:51 pm #1543283Hi Martin
RG probably knows this method, but perhaps, like me, forgot about the good old DOS pipe method to send stuff to the clipboard.
Here’s the method:Code:Sub test() zVar = 123456.78966666 '<<any vba variable value you want Set zShell = CreateObject("WScript.Shell") zShell.Run ("%comspec% /c echo " & zVar & " | clip"), vbHide End Sub
..if you run this code, you can then use [Ctrl][v] to paste the clipboard value wherever you want.
zeddy
-
macropod
AskWoody_MVPDecember 23, 2015 at 7:20 pm #1543327I can do this easily enough by writing the variable to a cell and then using Range.copy (and then erasing the cell contents), but I am surprised there doesn’t seem to be a simple direct way of doing this.
Or am I wrong ?Perhaps you could explain why you want to put the variable’s value into the clipboard?
Cheers,
Paul Edstein
[Fmr MS MVP - Word]Maudibe
AskWoody_MVPWSMartinM
AskWoody LoungerDecember 24, 2015 at 3:55 am #1543416Good question Paul, good answer Maud !
I have to enter data into a website from time to time, from various places in a spreadsheet – most of the cells in question are protected (albeit by me).
So I’ve created a button with an assigned macro (which has a keyboard shortcut) which copies the value of any selected cell to the clipboard. To help me avoid errors, the macro displays the value about to be pasted to a message box before it is copied to the clipboard.
As ever, an extraordinary number of different solutions have been offered here – my on-going surprise is that none of them, including mine, is particularly memorable when coding. I think I’ll have to create a UDF to get around that as I’m finding it quite a useful function.
Viewing 10 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Netplwiz not working
by
RetiredGeek
4 hours, 39 minutes ago -
Windows 11 24H2 is broadly available
by
Alex5723
7 hours, 11 minutes ago -
Microsoft is killing Authenticator
by
Alex5723
5 hours, 45 minutes ago -
Downloads folder location
by
CWBillow
13 hours, 38 minutes ago -
Remove a User from Login screen
by
CWBillow
11 minutes ago -
TikTok fined €530 million for sending European user data to China
by
Nibbled To Death By Ducks
4 hours, 43 minutes ago -
Microsoft Speech Recognition Service Error Code 1002
by
stanhutchings
4 hours, 45 minutes ago -
Is it a bug or is it expected?
by
Susan Bradley
9 hours, 24 minutes ago -
Image for Windows TBwinRE image not enough space on target location
by
bobolink
3 hours, 54 minutes ago -
Start menu jump lists for some apps might not work as expected on Windows 10
by
Susan Bradley
1 day, 3 hours ago -
Malicious Go Modules disk-wiping malware
by
Alex5723
17 hours, 33 minutes ago -
Multiple Partitions?
by
CWBillow
18 hours, 13 minutes ago -
World Passkey Day 2025
by
Alex5723
1 day, 11 hours ago -
Add serial device in Windows 11
by
Theodore Dawson
2 days, 2 hours ago -
Windows 11 users reportedly losing data due forced BitLocker encryption
by
Alex5723
3 hours, 36 minutes ago -
Cached credentials is not a new bug
by
Susan Bradley
2 days, 7 hours ago -
Win11 24H4 Slow!
by
Bob Bible
2 days, 7 hours ago -
Microsoft hiking XBox prices starting today due to Trump’s tariffs
by
Alex5723
2 days, 4 hours ago -
Asus adds “movement sensor” to their Graphics cards
by
n0ads
2 days, 9 hours ago -
‘Minority Report’ coming to NYC
by
Alex5723
2 days, 6 hours ago -
Apple notifies new victims of spyware attacks across the world
by
Alex5723
2 days, 18 hours ago -
Tracking content block list GONE in Firefox 138
by
Bob99
2 days, 17 hours ago -
How do I migrate Password Managers
by
Rush2112
2 days, 1 hour ago -
Orb : how fast is my Internet connection
by
Alex5723
2 days, 3 hours ago -
Solid color background slows Windows 7 login
by
Alex5723
3 days, 6 hours ago -
Windows 11, version 24H2 might not download via Windows Server Updates Services
by
Alex5723
3 days, 4 hours ago -
Security fixes for Firefox
by
Susan Bradley
5 hours, 46 minutes ago -
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
3 days, 16 hours ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
4 days, 1 hour ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
3 days, 16 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.