Shape.SetFormulas 方法 (Visio)

设置一个或多个单元格的公式。

语法

expression. SetFormulas( _SRCStream()_ , _formulaArray()_ , _Flags_ )

expression 一个代表 Shape 对象的变量。

参数

名称 必需/可选 数据类型 说明
SID_SRCStream () 必需 Integer 用于标识要修改的单元格的流。
formulaArray () 必需 Variant 分配给被标识的单元格的公式。
Flags 必需 Integer 影响 SetFormulas 行为的标志。

返回值

整数

备注

SetFormulas 方法与 Formula 属性类似,不同之处在于您可以使用该方法同时设置许多单元格的公式,而不是一次只能设置一个单元格的公式。

对于 Shape 对象,您可以使用 SetFormulas 方法设置任意一组单元格的结果。 您可以通过在 SID_SRCStream() 中传递一组整数来告诉 SetFormulas 方法您要设置哪些单元格。 SID_SRCStream() 是一个由 2 字节的整数组成的一维数组。

对于 Shape 对象,SID_SRCStream () 应该是 n = 1 的 3 n 个 2 字节整数的>一维数组。 SetFormulas 方法将流解释为:

{sectionIdx, rowIdx, cellIdx }n

其中 sectionIdx 是所需单元格的内容索引,rowIdx 是它的行索引,cellIdx 是它的单元格索引。

formulaArray () 参数应为 1 <= m 变体的一维数组。 每个 Variant 应为 String、对 String 的引用或 Empty。 如果 formulaArray (i) 为空,则 第 i 个单元格将设置为 formulaArray (j) 中的公式,其中 j 是最近不为空的前一项的索引。 如果前面没有非空项,则相应的单元格不会更改。 如果指定的公式少于单元格 ( m<n ) ,则 i'th 单元格 i>m 将设置为与选择将 m'th 单元格设置为的公式相同。 因此,要将许多单元格设置为相同的公式,只需传递该公式的一个副本即可。

Flags 参数应该是下列值的位掩码。

常量 说明
visSetBlastGuards &H2 覆盖当前的单元格值,即使它们是受保护的。
visSetTestCircular &H4 检测循环单元格引用的建立。
visSetUniversalSyntax &H8 公式使用通用语法。

SetFormulas 方法返回的值是 SID_SRCStream() 中已成功处理的项数。 如果 i<n 个条目处理正确,但在 i + 1 号条目上出错, SetFormulas 方法将引发异常并返回 i。 否则,将返回 n

示例

以下宏演示如何使用 SetFormulas 方法。 它假定有一个活动 Microsoft Office Visio 页面,其上至少有三个形状。 它使用 GetFormulas 方法获取形状 1 的宽度、形状 2 的高度和形状 3 的角度。 然后,它使用 SetFormulas 将形状 1 的宽度设置为形状 2 的高度,将形状 2 的高度设置为形状 1 的宽度。 形状 3 的角度保持不变。

以下示例使用 Page 对象的 GetFormulas 方法获取三个单元格公式,并使用同一对象的 SetFormulas 方法设置这些公式。 对于每个单元格,输入数组有四个段,Master 对象同样如此。 对于 ShapeStyle 对象,每个单元格只需要三个段(节、行和单元格)。

 
Public Sub SetFormulas_Example() 
 
 On Error GoTo HandleError 
 
 Dim aintSheetSectionRowColumn(1 To 3 * 4) As Integer 
 aintSheetSectionRowColumn(1) = ActivePage.Shapes(1).ID 
 aintSheetSectionRowColumn(2) = visSectionObject 
 aintSheetSectionRowColumn(3) = visRowXFormOut 
 aintSheetSectionRowColumn(4) = visXFormWidth 
 
 aintSheetSectionRowColumn(5) = ActivePage.Shapes(2).ID 
 aintSheetSectionRowColumn(6) = visSectionObject 
 aintSheetSectionRowColumn(7) = visRowXFormOut 
 aintSheetSectionRowColumn(8) = visXFormHeight 
 
 aintSheetSectionRowColumn(9) = ActivePage.Shapes(3).ID 
 aintSheetSectionRowColumn(10) = visSectionObject 
 aintSheetSectionRowColumn(11) = visRowXFormOut 
 aintSheetSectionRowColumn(12) = visXFormAngle 
 
 'Return the formulas of the cells. 
 Dim avarFormulaArray() As Variant 
 ActivePage.GetFormulas aintSheetSectionRowColumn, avarFormulaArray 
 
 'Use SetFormulas to: 
 ' - Set the width of shape 1 to height of shape 2. 
 ' - Set height of shape 2 to width of shape 1. 
 ' Note: avarFormulaArray() is indexed from 0 to 2. 
 Dim varTemp As variant 
 varTemp = avarFormulaArray(0) 
 avarFormulaArray(0) = avarFormulaArray(1) 
 avarFormulaArray(1) = varTemp 
 
 'Pass the same array back to SetFormulas that we 
 'just passed to GetFormulas, leaving angle alone. By setting 
 'the sheet ID entry in the third slot of the 
 'aintSheetSectionRowColumn array to visInvalShapeID, 
 'we tell SetFormulas to ignore that slot. 
 aintSheetSectionRowColumn (9) = visInvalShapeID 
 
 'Tell Microsoft Visio to set the formulas of the cells. 
 ActivePage.SetFormulas aintSheetSectionRowColumn, avarFormulaArray, 0 
 
 Exit Sub 
 
HandleError: 
 
 MsgBox "Error" 
 
 Exit Sub 
 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。