用于存储单元格值数据并在单元格内容更改时保留值的宏
Macro to store cell value data and retain the value even when cell content changes
我需要永久存储一些单元格B4和B5的瞬时值。
我的问题是当单元格内容发生变化时,我存储的变量也会发生变化,但我希望变量保留它们在“Record_Instantaneous_Values”宏为 运行 时首先获取的值,即使在将新数据手动输入到 B4 和 B5 之后——基本上是为了建立一个永久记录,记录每次调用宏时 B4 和 B5 的值。
这是我的
' Global Variables....
Global FirstCell
Global SecondCell
' ...
Sub Record_Instantaneous_Values
FirstCell = ThisComponent.CurrentController.ActiveSheet.getCellByPosition( 1, 3 )
SecondCell = ThisComponent.CurrentController.ActiveSheet.getCellByPosition( 1, 4 )
End Sub
Sub Peek_at_stored_values
Print "FirstCell = "; FirstCell.value; ", "; "SecondCell = ";SecondCell.value
End Sub
LO 中有一个“撤消”功能,这意味着可以存储特定时刻的单元格内容(大概在某个数组中)。虽然不想深入研究,但必须有一些简单的方法来实现我需要的,但是如何实现?
这是将值存储在全局变量中的基本代码。
Type RecordedCellType
col As Integer
row As Integer
val As Single 'floating point value of cell
init As Boolean 'has the value been initially recorded?
End Type
Const NUM_CELLS_TO_RECORD = 2
Global CellValues
Sub Initialize_Recorded_Values
Dim CellValuesLocal(NUM_CELLS_TO_RECORD) As New RecordedCellType
CellValues = CellValuesLocal
For CellNum = 1 to NUM_CELLS_TO_RECORD
CellValues(CellNum).init = False
Next
CellValues(1).col = 1
CellValues(1).row = 3
CellValues(2).col = 1
CellValues(2).row = 4
Call Peek_at_stored_values
End Sub
Sub Record_Instantaneous_Values
oSheet = ThisComponent.CurrentController.ActiveSheet
For CellNum = 1 to NUM_CELLS_TO_RECORD
With CellValues(CellNum)
If .init = False Then
oCell = oSheet.getCellByPosition(.col, .row)
.val = oCell.getValue()
.init = True
End If
End With
Next
Call Peek_at_stored_values
End Sub
Sub Peek_at_stored_values
String sDisplay
For CellNum = 1 to NUM_CELLS_TO_RECORD
With CellValues(CellNum)
sDisplay = sDisplay & "Cell(" & .col & _
"," & .row & ") "
If .init = True Then
sDisplay = sDisplay & "= " & .val
Else
sDisplay = sDisplay & "not initialized"
End If
End With
If CellNum < NUM_CELLS_TO_RECORD Then
sDisplay = sDisplay & CHR$(13) 'newline
End If
Next
MsgBox sDisplay
End Sub
利用出色的 solution/code Jim K 发布,这里有一个相关的宏,可以将存储的值写回原始单元格(在按下表单控制按钮时)以供可能需要它的任何人使用。
Sub Restore_Stored_Values
oSheet = ThisComponent.CurrentController.ActiveSheet
For CellNum = 1 to NUM_CELLS_TO_RECORD
With CellValues(CellNum)
If .init = True Then
oCell = oSheet.getCellByPosition(.col, .row)
oCell.Value=.val
End If
End With
Next
End Sub
我已经有了很多关于这项技术非常有用的想法。
我需要永久存储一些单元格B4和B5的瞬时值。
我的问题是当单元格内容发生变化时,我存储的变量也会发生变化,但我希望变量保留它们在“Record_Instantaneous_Values”宏为 运行 时首先获取的值,即使在将新数据手动输入到 B4 和 B5 之后——基本上是为了建立一个永久记录,记录每次调用宏时 B4 和 B5 的值。
这是我的
' Global Variables....
Global FirstCell
Global SecondCell
' ...
Sub Record_Instantaneous_Values
FirstCell = ThisComponent.CurrentController.ActiveSheet.getCellByPosition( 1, 3 )
SecondCell = ThisComponent.CurrentController.ActiveSheet.getCellByPosition( 1, 4 )
End Sub
Sub Peek_at_stored_values
Print "FirstCell = "; FirstCell.value; ", "; "SecondCell = ";SecondCell.value
End Sub
LO 中有一个“撤消”功能,这意味着可以存储特定时刻的单元格内容(大概在某个数组中)。虽然不想深入研究,但必须有一些简单的方法来实现我需要的,但是如何实现?
这是将值存储在全局变量中的基本代码。
Type RecordedCellType
col As Integer
row As Integer
val As Single 'floating point value of cell
init As Boolean 'has the value been initially recorded?
End Type
Const NUM_CELLS_TO_RECORD = 2
Global CellValues
Sub Initialize_Recorded_Values
Dim CellValuesLocal(NUM_CELLS_TO_RECORD) As New RecordedCellType
CellValues = CellValuesLocal
For CellNum = 1 to NUM_CELLS_TO_RECORD
CellValues(CellNum).init = False
Next
CellValues(1).col = 1
CellValues(1).row = 3
CellValues(2).col = 1
CellValues(2).row = 4
Call Peek_at_stored_values
End Sub
Sub Record_Instantaneous_Values
oSheet = ThisComponent.CurrentController.ActiveSheet
For CellNum = 1 to NUM_CELLS_TO_RECORD
With CellValues(CellNum)
If .init = False Then
oCell = oSheet.getCellByPosition(.col, .row)
.val = oCell.getValue()
.init = True
End If
End With
Next
Call Peek_at_stored_values
End Sub
Sub Peek_at_stored_values
String sDisplay
For CellNum = 1 to NUM_CELLS_TO_RECORD
With CellValues(CellNum)
sDisplay = sDisplay & "Cell(" & .col & _
"," & .row & ") "
If .init = True Then
sDisplay = sDisplay & "= " & .val
Else
sDisplay = sDisplay & "not initialized"
End If
End With
If CellNum < NUM_CELLS_TO_RECORD Then
sDisplay = sDisplay & CHR$(13) 'newline
End If
Next
MsgBox sDisplay
End Sub
利用出色的 solution/code Jim K 发布,这里有一个相关的宏,可以将存储的值写回原始单元格(在按下表单控制按钮时)以供可能需要它的任何人使用。
Sub Restore_Stored_Values
oSheet = ThisComponent.CurrentController.ActiveSheet
For CellNum = 1 to NUM_CELLS_TO_RECORD
With CellValues(CellNum)
If .init = True Then
oCell = oSheet.getCellByPosition(.col, .row)
oCell.Value=.val
End If
End With
Next
End Sub
我已经有了很多关于这项技术非常有用的想法。