如何在VBA中将公式保存为字符串
如何在VBA中将公式保存为字符串
这个问题已经有了答案:
我有以下代码
Sub reset_formulas() uptime_formula = "=IFS(AND(C25="Deployed",C26="Returned"), B26-B25, AND(C25="Returned",C26="Deployed"), 0, AND(C25="Deployed",C26="Deployed"),TODAY()-B25, AND(C25="Returned",C26="Returned"), 0, AND(C25="Deployed",C26=""), TODAY()-B25, AND(C25="Returned",C26=""), 0)" End Sub
我一直得到一个\"语法错误\"的错误消息,我猜这是由于单词\"Deployed\" 和 \"Returned\"周围的\" \"字符引起的。
有什么简单的方法可以将公式保存为Excel中的字符串吗?
admin 更改状态以发布 2023年5月21日
公式很快就会变成一团糟。在代码中,您可以像这个例子一样将其分解成(子)行:
' Build a cell formula like: ' ' =SUMPRODUCT(ROUND(Tabel_1234[ColumnName1]*Tabel_1234[ColumnName2]*Tabel_1234[ColumnName3],2)) ' Public Function BuildSumproductResource( _ ByRef Table As ListObject, _ ByVal ColumnIndex1, _ ByVal ColumnIndex2, _ ByVal ColumnIndex3) _ As String Const Sum As String = "=SUMPRODUCT(ROUND(" Dim Formula As String Formula = Sum & _ Table.Name & _ "[" & Table.ListColumns(ColumnIndex1).Name & "]*" & _ Table.Name & _ "[" & Table.ListColumns(ColumnIndex2).Name & "]*" & _ Table.Name & _ "[" & Table.ListColumns(ColumnIndex3).Name & "]," & _ "2))" BuildSumproductResource = Formula End Function