Copying a VBA variable into the Clipboard

Warminster

New Member
Joined
Jun 12, 2002
Messages
46
Maybe this is an easy question, but I don't know how to do this.

I have a macro that copies a cell, and then uses VBA to modify that information. I'd like to be able to put that information into the clipboard, so that I can paste the information into any cell that I choose.

How do I copy my variable into the clipboard.
 
12 years later, I needed to copy a variable to the clipboard and found this thread.
Chip Pearson's page at Windows Clipboard provides the code.
Important step: in VBA, go to Tools, References, and add Microsoft Forms 2.0 Library as a reference.
Then, the following code will put the contents of the MS variable on the clipboard:
Dim DataObj As New MSForms.DataObject
DataObj.SetText MS
DataObj.PutInClipboard

For an example of this in action, my July 10 2014 blog post shows how to use the code to copy the statistics from the Status Bar to the clipboard for later pasting:
Copy the Quick Stats Values to the Clipboard

Code:
Sub CopyQuickStatsToClipboard()
    Set WF = Application.WorksheetFunction
    MS = "Average: " & vbTab & WF.Average(Selection) & vbCr _
       & "Count: " & vbTab & WF.CountA(Selection) & vbCr _
       & "Numerical Count: " & vbTab & WF.Count(Selection) & vbCr _
       & "Min: " & vbTab & WF.Min(Selection) & vbCr _
       & "Max: " & vbTab & WF.Max(Selection) & vbCr _
       & "Sum: " & vbTab & WF.Sum(Selection) & vbCr
    
    ' Thanks to Chip Pearson http://www.cpearson.com/excel/Clipboard.aspx
    Dim DataObj As New MSForms.DataObject
    DataObj.SetText MS
    DataObj.PutInClipboard
End Sub


Sub CopyQuickStatsAsFormulas()
    Set WF = Application.WorksheetFunction
    MA = Selection.Address
    MS = "Average: " & vbTab & "=AVERAGE(" & MA & ")" & vbCr _
       & "Count: " & vbTab & "=CountA(" & MA & ")" & vbCr _
       & "Numerical Count: " & vbTab & "=Count(" & MA & ")" & vbCr _
       & "Min: " & vbTab & "=Min(" & MA & ")" & vbCr _
       & "Max: " & vbTab & "=Max(" & MA & ")" & vbCr _
       & "Sum: " & vbTab & "=Sum(" & MA & ")" & vbCr _
    
    ' Thanks to Chip Pearson http://www.cpearson.com/excel/Clipboard.aspx
    Dim DataObj As New MSForms.DataObject
    DataObj.SetText MS
    DataObj.PutInClipboard
End Sub
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I don't think you need the 'MsForms.' bit anymore in XL2010... I can't check atm because XL isn't recognising it with or without it, full stop :(
 
Upvote 0

Forum statistics

Threads
1,223,674
Messages
6,173,746
Members
452,533
Latest member
Alex19k

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