[英]Excel/VBA update cell every time value in another cell changes
I'm very new to VBA. 我是VBA的新手。 I have a cell in the worksheet that live updates a value.
我在工作表中有一个单元格,可以实时更新值。 the Value changes about once every month.
该值大约每月更改一次。 Is there a way for me to record the day that the value changed in an adjacent cell?
有什么方法可以记录相邻单元格中值更改的日期? For example, If the value in A1 changes from 5 to 6 today, I just want to record today's date in A2.
例如,如果A1中的值今天从5变为6,我只想在A2中记录今天的日期。
I don't really need to keep a record of previous changes. 我真的不需要保留以前的更改记录。
Thank you so much! 非常感谢!
If you are using a Bloomberg function in cell A1 like BDP() or BDH() or BDS() , then you could use the Worksheet_Calculate()
event macro to detect changes in that cell. 如果在单元格A1中使用Bloomberg函数(例如BDP()或BDH()或BDS()) ,则可以使用
Worksheet_Calculate()
事件宏来检测该单元格中的更改。
In this example, I use cell A3 as a "memory" to avoid re-posting the date too often: 在此示例中,我将单元格A3用作“内存”以避免过多地重新发布日期:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If [A1] <> [A3] Then
[A3] = Range("A1").Value
[A2] = Date
MsgBox "Date recorded"
End If
Application.EnableEvents = True
End Sub
The Worksheet_Change()
is being fired when something on the sheet changes its value, so add something like this to your Sheet-Codemodule: 当
Worksheet_Change()
上的某项更改其值时,将触发Worksheet_Change()
,因此将以下内容添加到工作表代码模块中:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Debug.Print "A1 has been changed!"
'do something
End If
End Sub
Update: 更新:
It seems you need the calculate event also as you're using a formula. 似乎在使用公式时也需要calculate事件。 you could try something like this:
您可以尝试这样的事情:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
ActiveSheet.Calculate
DoEvents
With Range("A1")
.Value = .Value
DoEvents
End With
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Range("A2").Value = Date
End If
End Sub
You could use the Worksheet_SelectionChange event. 您可以使用Worksheet_SelectionChange事件。 I guess it depends on what else you're doing in the worksheet as to whether it would fire or not.
我猜这取决于您是否会在工作表中执行其他操作。 You'd have to compare the value in A1 against what was in A1 previously which would be stored in another cell.
您必须将A1中的值与A1中先前存储在另一个单元格中的值进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.