Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to remember or save previous cell value of a changed cell in Excel?

Normally, when updating a cell with new content, the previous value will be covered unless undo the operation in Excel. However, if you want to keep the previous value for comparing with the updated one, saving the previous value into another cell or into the cell comment will be a good choice. The method in this article will help you to achieve it.

Save previous cell value with VBA code in Excel

Enable you to do smarter, faster and better

Kutools - the best office productivity tool brings 300 advanced features to Excel. Details... Download...

Office Tab - brings tabbed interface to Excel and Word, make your work much easier. Details...Download...


Save previous cell value with VBA code in Excel


Supposing you have a table as below screenshot shown. If any cell in column C changed, you want to save its previous value into the corresponding cell of column G or save in comment automatically. Please do as follows to achieve it.

1. In the worksheet contains the value you will save when updating, right-click the sheet tab and select View Code from the right-clicking menu. See screenshot:

2. In the opening Microsoft Visual Basic for Applications window, copy the below VBA code into the Code window.

The following VBA code helps you saving previous cell value of specified column into another column.

VBA code: Save previous cell value into another column cell

Dim xRg As Range
Dim xChangeRg As Range
Dim xDependRg As Range
Dim xDic As New Dictionary
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim I As Long
    Dim xCell As Range
    Dim xDCell As Range
    Dim xHeader As String
    Dim xCommText As String
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    xHeader = "Previous value :"
    x = xDic.Keys
    For I = 0 To UBound(xDic.Keys)
        Set xCell = Range(xDic.Keys(I))
        Set xDCell = Cells(xCell.Row, 7)
        xDCell.Value = ""
        xDCell.Value = xDic.Items(I)
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim I, J As Long
    Dim xRgArea As Range
    On Error GoTo Label1
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    Set xDependRg = Target.Dependents
    If xDependRg Is Nothing Then GoTo Label1
    If Not xDependRg Is Nothing Then
        Set xDependRg = Intersect(xDependRg, Range("C:C"))
    End If
Label1:
    Set xRg = Intersect(Target, Range("C:C"))
    If (Not xRg Is Nothing) And (Not xDependRg Is Nothing) Then
        Set xChangeRg = Union(xRg, xDependRg)
    ElseIf (xRg Is Nothing) And (Not xDependRg Is Nothing) Then
        Set xChangeRg = xDependRg
    ElseIf (Not xRg Is Nothing) And (xDependRg Is Nothing) Then
        Set xChangeRg = xRg
    Else
        Application.EnableEvents = True
        Exit Sub
    End If
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Formula
        Next
    Next
    Set xChangeRg = Nothing
    Set xRg = Nothing
    Set xDependRg = Nothing
    Application.EnableEvents = True
End Sub

For saving the previous cell value in a comment, please apply the below VBA code

VBA code: Save previous cell value in the comment

Dim xRg As Range
Dim xChangeRg As Range
Dim xDependRg As Range
Dim xDic As New Dictionary
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim I As Long
    Dim xCell As Range
    Dim xHeader As String
    Dim xCommText As String
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    xHeader = "Previous value :"
    For I = 0 To UBound(xDic.Keys)
        Set xCell = Range(xDic.Keys(I))
        If Not xCell.Comment Is Nothing Then xCell.Comment.Delete
        With xCell
            .AddComment
            .Comment.Visible = False
            .Comment.Text xHeader & vbCrLf & xDic.Items(I)
        End With
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim I, J As Long
    Dim xRgArea As Range
    On Error GoTo Label1
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    Set xDependRg = Target.Dependents
    If xDependRg Is Nothing Then GoTo Label1
    If Not xDependRg Is Nothing Then
        Set xDependRg = Intersect(xDependRg, Range("C:C"))
    End If
Label1:
    Set xRg = Intersect(Target, Range("C:C"))
    If (Not xRg Is Nothing) And (Not xDependRg Is Nothing) Then
        Set xChangeRg = Union(xRg, xDependRg)
    ElseIf (xRg Is Nothing) And (Not xDependRg Is Nothing) Then
        Set xChangeRg = xDependRg
    ElseIf (Not xRg Is Nothing) And (xDependRg Is Nothing) Then
        Set xChangeRg = xRg
    Else
        Application.EnableEvents = True
        Exit Sub
    End If
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Text
        Next
    Next
    Set xChangeRg = Nothing
    Set xRg = Nothing
    Set xDependRg = Nothing
    Application.EnableEvents = True
End Sub

Note: In the code, number 7 indicates the column G you will save the previous cell into, and C:C is the column you will save the previous cell value. Please change them based on your needs.

3. Click Tools > References to open the Refereces – VBAProject dialog box, check the Microsoft Scripting Runtime box, and finally click the OK button. See screenshot:

4. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.

From now on, when cell value in column C updated, the previous value of the cell will be saved into corresponding cells in column G, or save in comment as below screenshots showed.

Save previous value into another cell:

Save previous value in comment:



Excel Productivity Tools

Kutools for Excel - The Best Office Productivity Tool

  • Brings 300 powerful advanced features to Excel, enable you to do smarter, faster and better.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Office Tab - brings tabbed interface to Office, make your work much easier.

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    OscarLopez · 2 months ago
    I was thinking about in a sentence "If/else" but I´m new using VBA, so if you have another post that could me help me, please share with me, and again thank you ! keep sharing the knowledge
  • To post as a guest, your comment is unpublished.
    OscarLopez · 2 months ago
    Hi ! I just wanted to know if it is possible register multiples changes to the cell, I mean, if I put data in the cell C2 and then I change that data for other information, the previous data pass to the cell G2 (like in this post), but If I change the value one more time in cell C2, the second change that I made pass to the cell H2 (for example) and now I registered the information of the 3 movements that I realized, and do it almost 5 times more (save the previous cell value 5 times). If you could help me I would appreciate so much because here in your post it´s the only place that I found where solve my problem partialy. Thanks for sharing this content!!!!

  • To post as a guest, your comment is unpublished.
    Gustavo Vier · 11 months ago
    i need something like this, but only in specific cells (ex.: G12 to show in H23 the old value)
    • To post as a guest, your comment is unpublished.
      Gustavo Vier · 11 months ago
      And other ... I need this run when a cell change by a result (EX.: A1 + B1 = C1... if I change A or B value, the script does not work - nothing happens in G cell)