简体   繁体   English

每次更改另一个单元格中的值时,Excel / VBA更新单元格

[英]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.

相关问题 如何使用NOW时间在Excel VBA中更新单元格以进行另一个单元格更新,而另一个单元格更新又是通过其他单元格更改来更新的? - How to update a cell in Excel VBA with NOW time for a another cell update which in turn was updated via other cell changes? Excel VBA 在给定另一个单元格中的值的情况下搜索单元格中的关键字列表,然后对第三个单元格进行更改 - Excel VBA to search for list of keywords in a cell given a value in another cell and then make changes to a third cell 需要根据 excel vba 列表中的另一个单元格值更新单元格中的值 - Need to update a value in a cell based on another cell value from the list in excel vba VBA Excel-根据其他单元格值更新单元格值 - VBA Excel - Update Cell value based on other cell value 当单元格在 VBA 中更改值时操作另一个单元格 - Manipulating another cell when a cell changes value in VBA 使用 VBA 根据另一个单元格值设置 Excel 单元格值 - Setting Excel cell value based on another cell value using VBA Excel VBA - 删除时间值小于的单元格 - Excel VBA - delete cell with time value less then 如果单元格值包含在另一个工作表中,则为Excel vba - Excel vba if cell value is included in another worksheet 使用VBA将单元格值复制到Excel中的另一个工作表 - Copying Cell Value to another sheet in Excel with VBA Excel VBA-将单元格值(如果为空)复制到同一行中的另一个单元格 - Excel VBA - Copy Cell Value, if empty, to another Cell in the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM