Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

VBA for Save old values

0

Hi,

I have values in range A1:A50. When change any value in this range get copy OLD value to range B1:B50.

Example: If A5 value change from 10 to 15 ,B5 will be 10 until change again  A5 value.

Thanx for your helps..

Answer
Discuss

Answers

0
Selected Answer

Hi Gargamel and welcome to the Forum

You can achieve that using the Worksheet_Change event (if you'e careful!).

Put this code behind the sheet you need it to operate on. I've added comments to explain how it works:

Private Sub Worksheet_Change(ByVal Target As Range)

' Quit if change isn't in range
If Intersect(Target, Range("A1:A50")) Is Nothing Then Exit Sub
'Otherwise...
Application.EnableEvents = False ' prevent second trigger of this code
myChange = Target.Value ' grab new value
Application.Undo 'restore old value
Target.Offset(0, 1) = Target.Value 'copy old to column B
Target.Value = myChange 'restore new value to column A
Application.EnableEvents = True ' restore events

End Sub
Note that if neighbouring cells in columns A and B are empty at first, nothing will seem to happen (since the blank cell is copied to column B).

If however you have cells with formula (as your discussion indicates) then provided they refer to cells in the same sheet, use this macro (which uses the Calculate event) instead. I've added comments and shown the bits in bold which might be changed to cover a bigger range or place values in a different column:

Private Sub Worksheet_Calculate()

Dim HistoryRg As Range, OldVals As Variant

Set HistoryRg = Range("A1:A50") 'set the range (in a single column) whose history will be recorded. 

Application.EnableEvents = False 'prevent a recalculation during the running of this code
    Application.Undo 'undo the change then store values to an array
    OldVals = Application.Transpose(Application.Transpose(HistoryRg))
    Application.Undo ' restore the changed values
    HistoryRg.Offset(0, 1).Value = OldVals ' paste the pre-change values to the offset column
Application.EnableEvents = True 'restore events

End Sub
 

Hope this works for you.

Discuss
0

Thankyou for your answer.

İ have a probelm with your solution. Exa. in A1 , i cant use any formula. like ( E1+D1). I cant add any formula in  column A

Discuss

Discussion

Gargamel

Firstly to reply to my Answer, please use the Discussion button  (under the Answer) since Answers should only be suggested solutions.

Sorry but I didn't understand from your Question that you might have a formula in some cells-it's always best to provide a sample workbook so we can understand your question completely. Equally I can't revise my Answer tonight but you may get another answer from another contributor. They don't always read the Discussion threads so it's better if you edit your original question to clarify the cells may contains a formula (and preferably add an Excel file for completeness). 
John_Ru (rep: 6152) May 14, '21 at 1:11 pm
Gargamel,
No. You won't get another answer from another contributor because you already selected an answer. This is a great disservice not only to yourself but also to the forum community because answers that are marked "Selected" will be seen as working and others will try to use them for their own purposes. This answer was marked as "good" by you but you say it doesn't work as you want. So, everybody loses.
Variatus (rep: 4889) May 14, '21 at 7:57 pm
@Variatus

Gargamel is new to the Forum and selected my somewhat rushed answer after I made my discussion point above.

I see that I can't delete my answer  so is the only option for Gargamel to deselect my answer (so I can delete it)?

For (happy) family reasons I'm very busy this weekend and can't do anything here.
John_Ru (rep: 6152) May 15, '21 at 12:30 am
Gargamel

Please see my revised answer- the second part should cover what you have requested.

If not, please deselect my Answer and I'll delete it. You should also delete your Answer then others might provide a good solution.
John_Ru (rep: 6152) May 15, '21 at 4:41 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login