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. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
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_MVP -
WSMartinM
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
-
Does windows update component store “self heal”? (Awaiting moderation)
by
Mike Cross
22 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
39 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
4 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
45 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
3 hours, 21 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
3 hours, 22 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
10 hours, 23 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
11 hours, 29 minutes ago -
0Patch, where to begin
by
cassel23
5 hours, 31 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 1 hour ago -
89 million Steam account details just got leaked,
by
Alex5723
12 hours, 53 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 9 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
11 hours, 47 minutes ago -
Installer program can’t read my registry
by
Peobody
6 hours, 44 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
22 hours, 26 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 5 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 20 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 5 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 7 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day, 3 hours ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 10 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 11 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 16 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 21 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 21 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
18 hours, 30 minutes ago -
May 2025 updates are out
by
Susan Bradley
57 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 3 hours ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
3 days, 3 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.