Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the actual excel formula (not the result) to a text string

Tags:

excel

vba

I have been searching all over for a solution and I couldn't find one.

Let's say I type into cell A1: =if(A2>1,1,0)

Excel will interpret that and either return 1 or 0.

However, what I would like to do is literally convert that formula into the string: '=if(A2>1,1,0)'

If you press Ctrl+~ into Excel, you will see all the formulas. Basically what I want to do, in conclusion, is take all those formulas and make them strings.

(The purpose, if you care, is to compare these strings with another workbook to make sure I didn't delete anything)

like image 582
user330739 Avatar asked Apr 15 '13 23:04

user330739


People also ask

How do I get rid of formula but keep text?

Delete a formula but keep the resultsSelect the cell or range of cells that contains the formula. Click Home > Copy (or press Ctrl + C). Click Home > arrow below Paste > Paste Values.


1 Answers

You can make a user defined function for this.

Put this code into a Module (not a worksheet code sheet, class, or form)

Function GetFormula(rng As range, Optional asR1C1 As Boolean = False) As String
    If asR1C1 Then
        getFormula = rng.FormulaR1C1
    Else
        getFormula = rng.Formula
    End If
End Function

and call it in your worksheet like so

=GetFormula(A1)

or if you want the results as R1C1

=GetFormula(A1,TRUE)
like image 85
Brad Avatar answered Oct 03 '22 14:10

Brad