Range.Formula 属性 (Excel)Range.Formula property (Excel)

返回或设置一个 Variant 值,它表示采用 A1 样式记号和宏语言的对象公式。Returns or sets a Variant value that represents the object's formula in A1-style notation and in the macro language.

语法Syntax

表达式Formulaexpression.Formula

expression 一个表示 Range 对象的变量。expression A variable that represents a Range object.

备注Remarks

此属性对于 OLAP 数据源无效。This property is not available for OLAP data sources.

如果单元格包含一个常量,此属性返回该常量。If the cell contains a constant, this property returns the constant. 如果单元格为空,则此属性返回一个空字符串。If the cell is empty, this property returns an empty string. 如果单元格包含公式,则 Formula 属性将该公式作为字符串返回,所用格式与在公式栏中显示的格式相同(包括等号 (=))。If the cell contains a formula, the Formula property returns the formula as a string in the same format that would be displayed in the formula bar (including the equal sign (=)).

如果将单元格的值或公式设置为日期,Microsoft Excel 将验证该单元格的格式是否已设置为某个日期或时间数字格式。If you set the value or formula of a cell to a date, Microsoft Excel verifies that cell is already formatted with one of the date or time number formats. 如果未设置,Microsoft Excel 会将数字格式更改为默认的短日期数字格式。If not, Microsoft Excel changes the number format to the default short date number format.

如果指定区域是一维或二维区域,则可将公式指定为 Visual Basic 中相同维数的数组。If the range is a one- or two-dimensional range, you can set the formula to a Visual Basic array of the same dimensions. 同样,也可在 Visual Basic 数组中使用公式。Similarly, you can put the formula into a Visual Basic array.

如果为多单元格区域设置公式,则会用公式填充该区域所有的单元格。Setting the formula for a multiple-cell range fills all cells in the range with the formula.

示例Example

以下代码示例设置 Sheet1 中 A1 单元格的公式。The following code example sets the formula for cell A1 on Sheet1.

Worksheets("Sheet1").Range("A1").Formula = "=$A$4+$A$10"

示例代码提供方:MrExcel.com 的 Bill JelenSample code provided by: Bill Jelen, MrExcel.com

下列代码示例设置 Sheet1 中 A1 单元格的公式,以显示今天的日期。The following code example sets the formula for cell A1 on Sheet1 to display today's date.

Sub InsertTodaysDate() 
    ' This macro will put today's date in cell A1 on Sheet1 
    Sheets("Sheet1").Select 
    Range("A1").Select 
    Selection.Formula = "=text(now(),""mmm dd yyyy"")" 
    Selection.Columns.AutoFit 
End Sub

参与者简介About the contributor

MVP Bill Jelen 是有关 Microsoft Excel 的二十多本书的作者。MVP Bill Jelen is the author of more than two dozen books about Microsoft Excel. 他是 Leo Laporte 的 TechTV 的常客,也是 MrExcel.com 的主持人,该网站包含超过 300,000 个关于 Excel 的问题和答案。He is a regular guest on TechTV with Leo Laporte and is the host of MrExcel.com, which includes more than 300,000 questions and answers about Excel.

另请参阅See also

Range 对象Range Object

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.