Uncategorized Archives - My Blog https://vbastring.com/blog/category/uncategorized/ My WordPress Blog Thu, 17 Nov 2022 16:51:44 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 214916311 How To Run Macro (VBA) When The Cell Value Changes https://vbastring.com/blog/2020/12/09/how-to-run-macro-vba-when-the-cell-value-changes/ Wed, 09 Dec 2020 19:32:09 +0000 http://www.vbastring.com/blog/?p=963 In this post, you are going to find out how to detect when a cell value on your worksheet changes. So when my F7 value changes from “george” or blank to something else, a message box appears. Here is the code: Private Sub Worksheet_Change(ByVal Target As Range) Static blnDoneThis As Boolean Dim varOldValue As Variant […]

The post How To Run Macro (VBA) When The Cell Value Changes appeared first on My Blog.

]]>
In this post, you are going to find out how to detect when a cell value on your worksheet changes.

So when my F7 value changes from “george” or blank to something else, a message box appears.

Here is the code:

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Static blnDoneThis As Boolean
    Dim varOldValue As Variant
    Dim varNewValue As Variant
    
    'initialize a variable to regulate the Worksheet_Change event
    If blnDoneThis Then
        blnDoneThis = False
        Exit Sub
    End If
    
    'get the new value
    varNewValue = Target.Value
    
    'set our regulator to true
    blnDoneThis = True
    
    'time to get the old value and store it in a variable
    Application.Undo

    varOldValue = Target.Value
    
    'tell the regulator, it's ok
    blnDoneThis = True
    
    'check if the month has changed from the old value
    If varNewValue <> varOldValue Then
        MsgBox " value has changed"
    End If
    
    'set the cell equal to the new value
    Target.Value = varNewValue
    
    
End Sub

Let me know if you have questions.

The post How To Run Macro (VBA) When The Cell Value Changes appeared first on My Blog.

]]>
963