Cell.Formula 方法 (Word)Cell.Formula method (Word)

将包含指定公式的 = (Formula) 域插入到单元格中。Inserts an = (Formula) field that contains the specified formula into a table cell.

语法Syntax

expressionexpression. Formula( _Formula_ , _NumFormat_ )

expression:必需。expression Required. 代表单元格对象的变量。A variable that represents a 'Cell' object.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
FormulaFormula 可选Optional VariantVariant 希望 = (Formula) 域求值的数学公式。The mathematical formula you want the = (Formula) field to evaluate. 可以使用与电子表格类似的方式引用表格中的单元格。Spreadsheet-type references to table cells are valid. 例如,"=SUM(A4:C4)"指定第四行中的前三个值。For example, "=SUM(A4:C4)" specifies the first three values in the fourth row. 有关 = (Formula) 域的详细内容,请参阅域代码:= (Formula) 域。For more information about the = (Formula) field, see Field codes:= (Formula) field.
NumFormatNumFormat 可选Optional VariantVariant = (Formula) 域的结果的格式。A format for the result of the = (Formula) field. 有关可应用的格式类型的信息,请参阅"数字图片 (#)"域开关。For information about the types of formats you can apply, see Numeric Picture (#) field switch.

注解Remarks

只要至少一个包含单元格的上方或左侧插入点所在的单元格的值,则 formula 是可选的。Formula is optional as long as there is at least one cell that contains a value above or to the left of the cell that contains the insertion point. 如果插入点上方的单元格不包含值,插入的字段是 {= SUM(ABOVE)};如果单元格左侧插入的点,包含值时,插入的字段是 sum。If the cells above the insertion point contain values, the inserted field is {=SUM(ABOVE)}; if the cells to the left of the insertion point contain values, the inserted field is {=SUM(LEFT)}. 如果插入点上方的单元格和插入点左侧的单元格都不包含值,则 Word 将使用下列规则来确定插入哪一个 SUM 函数:If both the cells above the insertion point and the cells to the left of the insertion point contain values, Microsoft Word uses the following rules to determine which SUM function to insert:

  • 如果紧邻插入点上面的单元格中含有数值,则 Word 插入 {=SUM(ABOVE)}。If the cell immediately above the insertion point contains a value, Word inserts {=SUM(ABOVE)}.

  • 如果紧邻插入点上面的单元格中不包含数值,而紧邻插入点左面的单元格中含有数值,则 Word 插入 {=SUM(LEFT)}。If the cell immediately above the insertion point doesn't contain a value and the cell immediately to the left of it does, Word inserts {=SUM(LEFT)}.

  • 如果紧邻单元格中均不包含数值,则 Word 插入 {=SUM(ABOVE)}。If neither adjoining cell contains a value, Word inserts {=SUM(ABOVE)}.

  • 如果不指定公式和所有上方和插入点左边的单元格为空,则域的结果时出错。If you don't specify Formula and all the cells above and to the left of the insertion point are empty, the result of the field is an error.

示例Example

本示例在活动文档的开始处创建一个 3x3 表格,然后计算第一列的平均值。This example creates a 3x3 table at the beginning of the active document and then averages the numbers in the first column.

Set myRange = ActiveDocument.Range(0, 0) 
Set myTable = ActiveDocument.Tables.Add(myRange, 3, 3) 
With myTable 
 .Cell(1,1).Range.InsertAfter "100" 
 .Cell(2,1).Range.InsertAfter "50" 
 .Cell(3,1).Formula Formula:="=Average(Above)" 
End With

本示例在插入点插入一个公式,该公式将决定选定单元格上面的单元格中的最大数值。This example inserts a formula at the insertion point that determines the largest number in the cells above the selected cell.

Selection.Collapse Direction:=wdCollapseStart 
If Selection.Information(wdWithInTable) = True Then 
 Selection.Cells(1).Formula Formula:="=Max(Above)" 
Else 
 MsgBox "The insertion point is not in a table." 
End If

另请参阅See also

Cell 对象Cell Object