You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VBA实现Excel中B2自动更新为A列最新输入值?

VBA Solution to Auto-Update B2 with Latest Column A Value

To reliably update cell B2 with the most recently entered/modified value in column A, use the worksheet's Worksheet_Change event. This triggers immediately when edits are made to column A, avoiding the inconsistencies of NOW().

Step-by-Step Implementation:

  • Right-click the tab of the worksheet you want this to work on (e.g., Sheet1).
  • Select View Code to open the VBA editor for that worksheet.
  • Paste the following code into the editor window:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim changedCells As Range
    Set changedCells = Intersect(Target, Me.Columns("A"))
    
    If Not changedCells Is Nothing Then
        ' Get the last modified cell in the changed range of column A
        Dim latestCell As Range
        Set latestCell = changedCells.Cells(changedCells.Cells.Count)
        ' Update B2 with the latest value
        Me.Range("B2").Value = latestCell.Value
    End If
End Sub

How It Works:

  • The Worksheet_Change event activates whenever any cell on the worksheet is edited.
  • Intersect(Target, Me.Columns("A")) checks if the edited cells are within column A.
  • If changes are detected in column A, we capture the last cell in the modified range (handling cases where multiple cells are edited at once, like pasting values) and assign its value to B2.

This ensures B2 always reflects the most recent edit in column A, with consistent, immediate updates.

内容的提问来源于stack exchange,提问作者Mrk234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:03:11