How do I get the old value of a changed cell in Excel VBA?

Cover Image for How do I get the old value of a changed cell in Excel VBA?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Tech Blog: How to Get the Old Value of a Changed Cell in Excel VBA

Have you ever found yourself in a situation where you need to know the old value of a cell that was just changed in Excel VBA? It can be frustrating when you're trying to track changes or perform certain actions based on previous values. But worry not, I've got you covered with a simple solution!

The Challenge

Let's say you have a worksheet with certain cells that you are interested in monitoring for changes. You want to capture the old value of the cell before it gets updated and perform some operations based on that value. In the provided code snippet, the Worksheet_Change event is used to detect changes in the desired range.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim old_value As String
    Dim new_value As String

    For Each cell In Target
        If Not (Intersect(cell, Range("cell_of_interest")) Is Nothing) Then
            new_value = cell.Value
            old_value = ' what here?
            Call DoFoo(old_value, new_value)
        End If
    Next cell
End Sub

You can see that the new_value is easily obtained from the cell.Value, but what about the old value? How can we retrieve it?

The Solution

To get the old value of a changed cell, we can leverage the Worksheet_Change event's Worksheet_Change(ByVal Target As Range) argument. This argument provides a reference to the range of cells that were changed.

We can utilize the Target argument to compare the current value of the cell against its previous value. By using the Worksheet_Change event's Application.Undo method, we can revert the cell to its previous state temporarily and retrieve its old value.

Let's modify the code snippet to include this solution:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim old_value As String
    Dim new_value As String

    For Each cell In Target
        If Not (Intersect(cell, Range("cell_of_interest")) Is Nothing) Then
            new_value = cell.Value
            Application.Undo
            old_value = cell.Value
            Call DoFoo(old_value, new_value)
        End If
    Next cell
End Sub

With the addition of Application.Undo before retrieving the value of the cell, we can temporarily revert it to its old value. After capturing the old value, we can perform any necessary actions using both the old and new values.

Call to Action

Now that you know how to retrieve the old value of a changed cell in Excel VBA, go ahead and implement it in your own projects! 💪

I hope this guide helps you overcome the challenge of obtaining old cell values. If you have any further questions or suggestions, please feel free to leave a comment below. 🔍😊

Happy coding! 🚀


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello