Macro to place a formula into clipboard?

FinancialAnalystKid

Well-known Member
Joined
Oct 14, 2004
Messages
779
Is it possible to create a macro to put a formula into the clipboard?

So with the next [enter] or [ctrl][v] it pastes into any selected cell?

Say the formula is a UDF like =TabName().
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
you can do this through a sub procedure:
Code:
Sub test()
ActiveCell.Copy
End Sub
But I am almost positive that you can not do this through a function.
 
Upvote 0
I wasn't very clear. Sorry.

I don't want the macro to copy a cell but to 'put' a formula into the clipboard. The formula is in the macro of course and not copied from anywhere else.
 
Upvote 0
Try this,

Code:
Private Sub CommandButton1_Click()
Dim MyData As Object
Set MyData = New DataObject
MyData.SetText ActiveCell.Formula
MyData.PutInClipboard
MsgBox MyData.GetText
End Sub

kind regards,
Erik
 
Upvote 0
FinancialAnalystKid,
sorry I didn't see your last post, but it's easy to change the macro

Code:
Private Sub CommandButton1_Click() 
Dim MyData As Object 
Set MyData = New DataObject 
MyData.SetText "=TabName()"
MyData.PutInClipboard 
MsgBox MyData.GetText 
End Sub
will this help?
Erik
 
Upvote 0
Oh, ok. I don't know how to copy VBA code through code, my guess is that you would have to build the formula as an object then copy the object. But how about if we use some blank real estate on your file:
Code:
Sub test()
Range("$AA$1").Value = "=20+sum($D$2)" 'replace with your formula
Range("$AA$1").Copy
End Sub
 
Upvote 0
erik.van.geit said:
FinancialAnalystKid,
sorry I didn't see your last post, but it's easy to change the macro

Code:
Private Sub CommandButton1_Click() 
Dim MyData As Object 
Set MyData = New DataObject 
MyData.SetText "=TabName()"
MyData.PutInClipboard 
MsgBox MyData.GetText 
End Sub
will this help?
Erik

Does this have to be in a workbook code or as a macro? Can this just be a public command?
 
Upvote 0
Very cool Erik!

Finance Kid,
Yes, just drop the code into a sub like this.
Code:
Sub formula2clipboard()
Dim MyData As Object
Set MyData = New DataObject
MyData.SetText "=TabName()" 'change to whatever your formula is
MyData.PutInClipboard
MsgBox MyData.GetText
End Sub
 
Upvote 0
Does this have to be in a workbook code or as a macro? Can this just be a public command?

I wrote it as a test with a button, but you can do this in any module.

Sub put_in_clipboard()
........ (code form previous post)
End Sub

kind regards,
Erik

EDIT: it's strange: I didn't see yankee428's reply when clicking to REPLY myself
 
Upvote 0
JFYI - the DataObject is in the MSForms library. So you need to have a reference to that in your project in order for this to work.

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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