保存单元格的值

我想在Excel中保存一个单元格的值。

这就是即时通讯使用的代码:

Dim oldval As Range Sub Macro0() Set oldval = ActiveCell End Sub Sub Macro2() Dim y As Integer Dim x As Variant Dim rng2 As Range Set rng2 = ActiveCell Dim rng As Range Set rng = ActiveCell If rng.Column = 8 And rng.Value <> "" Then 'Extract the number part of string x = Split(rng2.Value, "_")(0) y = Split(rng.Value, "_")(0) If y < 1 Or x = "" Then Exit Sub If x < y Then MsgBox "Attenzione, si sta decrementando di stato un fornitore !" Else MsgBox "Stato cambiato con successo" End If End If End Sub 

我需要在更改之前保存单元格中的值。 我尝试使用该代码:

 Private Sub Worksheet_Change(ByVal Target As Range) Call Macro1 End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Call Macro0 End Sub 

但它不工作,因为即时通讯使用不在工作簿上的macros。 我需要做一个控制后,我已经保存了旧的值,并检查是否高于已插入的新值。 我怎样才能保存旧的价值? 谢谢

编辑:@Jeeped多数民众赞成如何看起来该文件 文件

我想我现在已经完全明白你的需要了。 这是一个解决方法:

在你的模块中,像这样添加macro1()

 Sub macro1(ByVal oldVal As String, ByVal newVal As String) MsgBox "The value was " & oldVal & ", but now is " & newVal End Sub 

在工作表模块中,添加以下两个事件+声明

 Dim oldVal As String, newVal As String 'the top declaration allows you to use the variables even when the macro/method of the worksheet has finished to run Private Sub Worksheet_Change(ByVal Target As Range) newVal = Target.Value 'this will just take the current value of the cell Call macro1(oldVal,newVal) 'they are both passed to your procedure End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) oldVal = Target.Value 'this will record the value of the cell everytime you are selecting it End Sub 

以下是它的工作方式,将单元格的内容从“buongiorno”更改为“buonasera”:

在这里输入图像说明

在这里输入图像说明

当然这只是一个例子,但是你只需要在代码中重新调整解决方法,它应该可以正常工作。 注意:我刚刚发现messagebox是用不同的语言显示消息,而不是编码,这是因为我在testing方法时做了一个截图。 对不起,如果你认为这可能会造成混乱,请提出编辑。

您将需要在项目级别声明该variables。 这是一个简单的例子:

 Dim a As String Sub macro1() a = Range("A1") End Sub Sub macro2() MsgBox a End Sub 

如果运行macro1,则variablesa(在顶层声明)将存储单元格Range(“A1”)的值。 然后,如果您在第二时间运行macro2,则该值将仍然存在。

您只需要在项目顶部声明variablesoldval ,并在您做出更改之前将oldValue = ActiveCell.Value存储为旧值。 即使离开macros,variables也会保持这个值,所以你可以使用这个值来比较它。