You are here:

Excel/change by val target


I am using excel 2003 using VBA.

In sheet1 a third party program is feeding a value into A1.

I would like to store the current A1.value in B1 so when the A1.value changes I can tell if the  B2.value > or < A2.value and store the difference in C2.

I believe this has to be a private sub but I'm not sure.

Kind regards,


Sounds like you want to use the change event.  

Right click on the sheet tab for the sheet where you want this behavior and choose View Code from the popup menu.  

This will take you to the visual basic editor and you will see the code module associated with your sheet.  At the top of the module will be two dropdowns.  In the left dropdown, select worksheet.  In the right dropdown select Change

this will put in the change event procedure.

Private Sub Worksheet_Change(ByVal Target As Range)

End Sub

(there may be a selectionchange event in the module, but that is NOT the one you want.

The argument to the Change Event is "Target".  This will be a range reference to the cell or cells that were changed.  You can then test if it is cell A1 and if so, then you can test the value of B1

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then Exit Sub
If Target.Value > Range("B1").Value Then
 ' it is bigger; what do you want to do
ElseIf Target.Value < Range("B1").Value Then
 ' it is smaller; what do you want to do
 ' it is equal; what do you want to do
End If
' now the test has been performed.  Write the value of A1 to B1?
Application.EnableEvents = False
 Range("B1").Value = Range("A1").Value
Application.EnableEvents = True
End Sub

This sample code should give you some idea of how to approach the problem.

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.