我在 LibreOffice Calc 中编写了一个宏,它能够正确运行。但如果我关闭文件并重新打开,它总是显示#NULL!
而不是正确的值。我在这里缺少什么?
我的宏代码
Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Function Calculate(CalType As String) As Double
'
' Calculate Macro
'
Dim i As Integer
Calc = 0
i = 1
Do While Not IsEmpty(Cells(i, 2))
If (Cells(i, 3).Value = CalType And (Cells(i,2) = "A" Or Cells(i,2) = "B")) Then
Calculate = Calculate + Cells(i, 4).Value
ElseIf (Cells(i, 3).Value = CalType And Cells(i,2) = "C") Then
Calculate = Calculate - Cells(i, 4).Value
End If
i = i + 1
Loop
'
End Function
调用函数将类似于=Calculate(J6)
文件另存为.ods
format.
The Cells
电话对我来说根本不起作用。它来自 VBA,而不是 LO Basic。但我认为这不是主要问题。
LibreOffice 期望用户定义的函数很简单,仅访问包含公式的单元格。由于调用该函数时电子表格尚未完全加载,因此无法读取其他单元格。
解决方法是忽略错误并等到文档完全加载后再运行该函数。以下面的代码为例:
Function ReadOtherCell(row, col)
On Error GoTo ErrorHandler
oSheet = ThisComponent.CurrentController.ActiveSheet()
oCell = oSheet.getCellByPosition(row, col)
ReadOtherCell = "value is '" & oCell.getString() & "'"
Exit Function
ErrorHandler:
Reset
End Function
Sub RecalculateAll
' This is for user-defined functions that need to read the spreadsheet.
' Assign it to the "View created" event,
' because before that, the spreadsheet is not fully loaded.
ThisComponent.calculateAll
End Sub
Enter foo
在 A1 中,以及=ReadOtherCell(0,0)
在A2中。到目前为止,这有同样的问题——第一次打开文档时会失败。
现在,前往Tools -> Customize
。在“事件”选项卡中,突出显示View created
。按Macro...
并找到RecalculateAll
功能。然后按确定。
现在,当文档关闭并重新打开时,单元格 A2 应该显示结果value is 'foo'
.
这源自 B. Marcelly 的回答.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)