Excel可以同时显示公式及其结果吗?


10

我知道在Excel中可以在显示值和显示公式之间切换。我需要在分配来转了统计类作为表示印刷Excel工作表两者的公式和结果。现在,教师使我们要么复制公式并将其粘贴为文本到计算值旁边,要么复制值并将其粘贴到公式旁边。这是非常低效的,容易出错(如果在复制粘贴后更改公式或值),通常会浪费时间。

有什么办法让Excel显示公式,并在同一小区中的价值?如果不是,是否有任何函数会将引用的单元格中的公式显示为纯文本,例如=showformula(A1),将打印出来=sum(A2:A5) 而不是25(如果这些是单元格的公式和值A1)?

我正在使用Excel 2010,但是一个适用于任何最新版本的Excel的通用答案都不错。

Answers:


18

我不知道有任何内置函数。但是,您可以创建用户定义的VB函数来完成所需的操作。

Alt + F11打开VBA编辑器,右键单击Project Explorer,然后选择Insert- > Module。粘贴以下代码:

Function GetFormula(cell)  
  GetFormula = cell.Formula  
End Function

现在,您可以=GetFormula(A1)用来显示该单元格的公式。


我使用了此例程-我收到一个错误未定义的变量。getformula的变量声明应该是什么?
Prasanna 2015年

@Prasanna GetFormula不是变量,而是函数。您正在使用哪个版本的Excel?
Indrek

Excel2013。请原谅我-我已经开始学习VBA
Prasanna 2015年

2

这是一种自动化您之前所做的方法。它结合了LonelyKnight对他/她自己的问题,Indrek的答案和Billis的答案的最好的部分:

Sub Show_Formulas()
    Dim rng As Range, cell As Range
    row_offset = 10
    col_offset = 0
    Set rng = Range(Cells(1, 1), Cells(5, 7))

    On Error GoTo ErrHandler
    For Each cell In rng
        cell.Offset(row_offset, col_offset) = "'" & cell.Formula
    Next cell
    Exit Sub

ErrHandler:
    MsgBox "The cow jumped over the moon", , "Error!", Err.HelpFile, Err.HelpContext

End Sub

对于指定范围内的每个单元格(此处硬编码为A1:G5),它将单元格的公式(受撇号保护)复制到固定偏移量的单元格中。(有关 在Excel中使用VBA的指导,请参阅如何在MS Office中添加VBA。)您只需要记住在打印之前运行此宏即可。

或者,PrintOut , , , True在之前添加一条语句Exit Sub,此宏将为您打印工作表。(您只需要记住使用此宏进行打印即可。)第四个参数PrintOutPreview,设置为True使Microsoft Excel在打印工作表之前调用打印预览(从而为您提供取消选项)或False(或省略)打印工作表。立即无条件地打印。

或者,如果你真的关心忘记运行此,您可以使用Private Sub Worksheet_Change更新显示的公式,你做的任何时间的任何工作表中的变化。


2

也许值得注意的是,从Excel 2013开始,有一个本机函数:FORMULATEXT

从功能上支持点办公室的.com文章FORMULATEXT

描述

以字符串形式返回公式。

句法

格式文字(参考)

FORMULATEXT函数语法具有以下参数:

  • 需要参考。对单元格或单元格范围的引用。

备注

  • 如果您选择引用的单元格,则FORMULATEXT函数将返回在编辑栏中显示的内容。

1
真好 内置是要走的路。与Indreks解决方案不同,如果该字段不是公式,则会显示错误,您可以在此之前轻松检查:= IF(ISFORMULA(B1); FORMULATEXT(B1); B1)
Peheje


0

免费的外接程序FormulaDesk可以为您执行此操作,并以更易理解的方式显示公式并查明公式中的错误。

如您在下面的屏幕快照中所见,您可以根据要求同时查看单元格的最终结果,原始公式以及该公式的增强型可探索显示。

[披露:我是FormulaDesk的作者]

在此处输入图片说明


1
感谢您披露您的隶属关系。看起来您在Stack Exchange上确实有过以前的贡献,这很棒,但是为了安全起见,我建议您重新阅读自我推广政策。您没有做错任何事情,但是当用户看到某个人的唯一答案是在推广自己制作的东西时,通常会将其标记为垃圾邮件。如果您可以包括屏幕截图作为示例,它也会改善此答案。谢谢!
nhinkle

谢谢您向我指出。我已经对答案进行了修改,以准确回答您的原始问题。
加雷斯·海特

0

我对编程知之甚少,我的回答也不是很优雅。作为未来的工程师,我需要Excel中的一个可以向我显示公式的函数。我在Indrek解决方案中发现的问题是,他的函数向我返回了这样的公式:“ C9 * C9 * pi()”,我希望将公式(C9)的显示中的单元格名称更改为的值。这些细胞;并且只要在原始C9单元中更改了这些数字,它就会自动更改。

我的代码具有非常基本的公式。我从没有在其他地方找到过这样的东西,它可能对某人有帮助。也许有人可以将其提高到一个不错的水平。就像我说的那样,它对我有用,而且我是一个编程技巧很少的实用人。

第一个功能根本不是我的。它是从以下位置检索到的:

/programming/10951687/how-to-search-for-string-in-an-array

Function IsInArray(ar, item$) As Boolean
    Dim delimiter$, list$

    ' Chr(7) is the ASCII 'Bell' Character.
    ' It was chosen for being unlikely to be found in a normal array.
    delimiter = Chr(7)

    ' Create a list string containing all the items in the array separated by the delimiter.
    list = delimiter & Join(ar, delimiter) & delimiter

    IsInArray = InStr(list, delimiter & item & delimiter) > 0
End Function

Function GETFORMULA(cell)

Dim alfabeto As String
Dim alfabetos() As String
Dim formula As String
Dim celda As String
Dim cutter As String
Dim cutterarray() As String


'The formula is taken from the cell
formula = cell.formula


'And alphabet is declared to be compared with the formula
alfabeto = "A,B,C,D,E,F,G,H,I,J,K,L,M,O,P,Q,R,S,T,U,V,W,X,Y,Z"


'An array is declared with the elements of the alphabet string
alfabetos = Split(alfabeto, ",")


'Replacing common functions with a symbol, otherwise the code will crash
formula = Replace(formula, "LOG", "#")
formula = Replace(formula, "SQRT", "?")
formula = Replace(formula, "PI", "ñ")

'MsgBox (formula)
Debug.Print formula

'Symbols to identify where the name of a cell might end are declared
cutter = "*,/,+,-,^,(,)"

'An array is declared with the symbols from the string that has been just declared
cutterarray = Split(cutter, ",")

Dim nuevaformula As String


'For the i position in the lenght of the formula
Dim index As Integer

For i = 1 To Len(formula)
    Debug.Print "Para i iterativo=", i
    Debug.Print "Se tiene el digito", Mid(formula, i, 1)



        'if the i element is not a letter, then add it to the chain of characters
        If IsInArray(alfabetos, Mid(formula, i, 1)) = False Then
        Debug.Print "Que es un numero"
        nuevaformula = nuevaformula + Mid(formula, i, 1)
        Debug.Print nuevaformula


        'if the element is a letter, then it´s necessary to get the name of the cell, found all the numbers
        'of the cell until a symbol to cut appears, like a: "*","/","+","-"
        Else
        Debug.Print "Encontramos una letra"
        'Empezamos a buscar más números
        'Numbers in the cell name are going to be find to isolate the cell number

            For s = 2 To 7
            celda = Mid(formula, i, s)
            Debug.Print "Incrementamos una posición quedando", celda

                If (i + s - 1 = Len(formula)) = False Then



                    'if a symbol to cut is not found in the last increment the last number hasn´t been reached
                    'and it´s necessary to keep loking for it
                    If IsInArray(cutterarray, Right(celda, 1)) = False Then
                    celda = Mid(formula, i, s)
                    Debug.Print "En el incremento no se encontró cutter. La i correcta es", i + s - 1


                    Else

                    'if it´s found the name of the cell is
                    celda = Mid(formula, i, s - 1)
                    Debug.Print "Se encontró un cutter la celda es", celda

                    'the search cycle has to be broken
                    Debug.Print s, i
                    i = i + s - 2
                    Debug.Print "Daremos salto a i=", i
                    Debug.Print celda
                    nuevaformula = nuevaformula + CStr(Range(celda).Value)
                    Exit For
                    End If
                Else
                Debug.Print "se alcanzó la máxima cantidad de iteraciones posibles"
                celda = Mid(formula, i, s)
                Debug.Print "La celda encontrada fue", celda
                nuevaformula = nuevaformula + CStr(Range(celda).Value)
                Debug.Print nuevaformula
                nuevaformula = Replace(nuevaformula, "#", "LOG10")
                nuevaformula = Replace(nuevaformula, "?", "raiz")
                nuevaformula = Replace(nuevaformula, "ñ", "pi")
                ISAAC = nuevaformula
                Debug.Print (nuevaformula)
                Exit Function
                End If


            'MsgBox (nuevaformula)
            Next s
        End If
Next i

nuevaformula = Replace(nuevaformula, "#", "LOG10")
nuevaformula = Replace(nuevaformula, "?", "raiz")
nuevaformula = Replace(nuevaformula, "ñ", "pi")

GETFORMULA = nuevaformula
Debug.Print (nuevaformula)
End Function


-2

这是一个解决方案:

  1. '在公式的开头加上撇号()
  2. 将其复制到下面的行或旁边的列
  3. 返回原始公式并删除撇号

这可行。


4
这需要在每次更改公式时重新复制该公式。频繁更改电子表格时,复制的公式与真实公式不同步将太容易了。
nhinkle

这是问题试图解决的确切情况。
亚历克斯M

-3

将文本与值合并假设单元格A1包含数字9999

="Total: "&TEXT(A1,"$#,##0.00")

该公式将显示“总计:$ 9,999.00”

另一个使用NOW函数显示当前日期/时间的文本的示例

="Report printed on: "&TEXT(NOW(),"mmmm d, yyyy at h:mm AM/PM")

2
除非我误解了您,否则这与我的要求完全无关。
nhinkle
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.