Getting Cell's Formula String with a Formula

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Getting Cell's Formula String with a Formula

Post by kpark91 »

Hello,

Is there an excel formula where I could get a formula a cell's?

Like If I want to get a formula of A1 as a string
=FORMULA(A1)

Just wondering if there was a function like that... Because in excel, it's either show all of the cells in formula or all of the cells in values O_o
I don't have one

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Getting Cell's Formula String with a Formula

Post by HansV »

There is no built-in function for that, but you could create it yourself.

If you need it in a single workbook:
- Press Alt+F11 to activate the Visual Basic Editor.
- Select Insert | Module.
- Enter or copy the following code into the module

Code: Select all

Function GetFormula(rng As Range) As String
  GetFormula = rng.Formula
End Function
Press Alt+F11 to activate Excel again. You can use the formula in the form

=GetFormula(A1)

or

=GetFormula(Sheet2!A1)

If you want to be able to use the function in all workbooks, you can put it in a module in your personal macro workbook Personal.xls(b) or in an add-in. If you put it in your personal macro workbook, you must use it like this:

=Personal.xls!GetFormula(A1)

for Excel 2003 or earlier, or

=Personal.xlsb!GetFormula(A1)

for Excel 2007 or later.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Getting Cell's Formula String with a Formula

Post by kpark91 »

Oh cool.

Thank you~

So, there is no built-in excel function like that? might have liked there to be :P
I don't have one

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Getting Cell's Formula String with a Formula

Post by HansV »

There is a function that returns all kinds of other information about a cell: the CELL function. But it doesn't have an argument to return the formula of a cell.
Best wishes,
Hans