5

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)

4

6 回答 6

12

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)
于 2013-04-16T00:35:57.580 回答
10

Use the FORMULATEXT function. It's built into Excel.

于 2015-02-26T16:07:39.253 回答
2

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

Simply use the Formula Property of the cell, here is a code example:

Range("A1").Select
Dim strFormula As String
strFormula  = ActiveCell.Formula
MsgBox (strFormula)
于 2013-04-15T23:55:34.990 回答
1

You can also replace the equal sign with blank using find and replace. In effect removing the equal sign and leaving only the text of the formula as a string.

于 2016-03-17T21:29:16.787 回答
0

Just follow this way.

  1. Select the range (contain formula) you want to convert to text, also you can select All range on your sheet.

  2. Use Find and Replace command by pressing Control-F then replace "=" with "^" press replace All.. All done....

  3. If you want to use it again as formula... Just use Find and Replace, then replace "^" with "=" just look....All formula in text format converted to formula...

于 2018-08-27T04:14:03.117 回答
-2

Just right-click on the column header (i.e. A) and select "Format Cells" then under the "Number" tab select "Text". Then at each cell press enter and the formula will be displayed instead of the result.

于 2013-11-13T16:04:34.737 回答