Copy Contents of the Formula Bar - not the Cell

mstuf

Active Member
Joined
Feb 4, 2003
Messages
321
Office Version
  1. 2016
Platform
  1. Windows
Good Evening -

I need a Macro that when run from the Active Cell, that would Highlight and Copy the <b>Entire</b> Formula Bar Contents to the Clipboard ?

I found a past thread here at Mr Excel:

http://www.mrexcel.com/forum/showthread.php?t=192384

It gives what appears to be a Solution to a similar need.

Code:
   Sub TryThisFirst()
   Dim DatObj As New DataObject
   DatObj.SetText ActiveCell.Text
   DatObj.PutInClipboard
End Sub

'add a reference to Microsoft Forms 2.X Object Library

I am not sure how to "add a reference to Microsoft Forms 2.X Object Library" to try this out. As is, it nets a Compile error at the Dim Statement.

Is this Code what I need ? Am I on the Right Track ?
Help and Explanation much appreciated. Or if there is a simpler way ?

Thanks as Always -- Your help appreciated much !!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
An easy way to establish a reference is to add and then delete a userform.

Code:
Sub TryThisFirst()
    Dim oDO      As MSForms.DataObject
    
    Set oDO = New MSForms.DataObject
    oDO.SetText ActiveCell.Formula
    oDO.PutInClipboard
End Sub
 
Upvote 0
You can interface the IDataObject interface at run time thus without the need to add a temp userform to your project.

use this routine :

Code:
Private Sub PutCellFormulaInClipBoard(ByVal Cell As Range)

    Dim oDataObject As Object
    
    Set oDataObject = _
        GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    With oDataObject
        .Clear
        .SetText Cell.Cells(1).Formula
        .PutInClipboard
    End With
    
End Sub

Usage:

The code below will place the Formula of the activecell into the clipboard.

Code:
Sub Test()
    PutCellFormulaInClipBoard ActiveCell
End Sub
 
Upvote 0
Thank You Shg

That Netted a Compile Error " User-defined type not defined "

Looking at Google I found similar error messages that say I need to "Set a Reference" --


Is that correct and can you tell me how ?

Thanks
 
Upvote 0
Jaafar Tribak --- thank you

I had never used a private sub before and it took some reading to figure out how to invoke it but once I grasp the method I was able to incorporate it into my routine.

Works Great ! Very Happy !! It will get used approx 70 times a week.

As I learn new stuff here -- it opens my eyes to more things I can do.



Shg -- Thank you for the Help too -- Jaafar Tribak's method is installed and working. Sorry that I didn't understand how to Set the Reference -- It gives me another thing to search and read during dinner. I will continue to read and try to learn.
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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