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

Store cell value and assign to another cell

0

Hi all, 

I have been using the below code to store the current value in C2 to a variable and assign it to C3 everytime C2 changes, to always see the last value. I'm using the below code but it hasn't worked for me till yet. any help will be appreciated. Thanks. 

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Current_Cell As Range

Dim Residuel_Cell As Range

Dim Holder As Variant

Set Current_Cell = Range("C2")

Set Residuel_Cell = Range("C3")

If Target.Address = Current_Cell.Address Then

Holder = Target.Value

    If Holder <> Target.Value Then

    Residuel_Cell.Value = Holder

    Holder = Current_Cell.Value

    End If

End If

End Sub

Answer
Discuss

Answers

0

Hello Gurbir and welcome to the Forum

Not sure why you're not setting the formula in C3 to "=C2" but your code has two successive lines where you firstly set the variable Holder to a value (Holder = Target.Value) then have a test to see if it is that value (If Holder <> Target.Value Then...) so the code that follows will never run.

If you just want to store C2's value in C3 (withiout using the formula), try this:

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("C2")) Is Nothing Then Exit Sub

Range("C3").Value = Target.Value


End Sub
The test in bold (above) means nothing happens unless it was C2 that changed.

If however your code declared the variable Holder for some other purpose (e.g. the new value to pass to other procedures) then you could either simply refer back to C2 (e.g. using Sheet1.Range("C2").Value) or a little more code will be needed.

In the code below, the variable is declared as a Public variable (so has "scope" outside the Worksheet_Change code) and this is illustrated by passing the varaible to another subroutine TestHolder which will print the changed value in VB Explorer's Intermediate window:

Public Holder As Variant

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("C2")) Is Nothing Then Exit Sub

Holder = Target.Value
Range("C3").Value = Target.Value

TestHolder

End Sub

Private Sub TestHolder()

Debug.Print Holder

End Sub
Notice that the Public declaration and TestHolder subs are outside the Worksheet_Change code. Clearly you would change the name and function of TestHolder  to do something useful!

Also I suggest you consider whether Holder needs to be delcared as a Variant (if you know what type of data is stored in C2 -especially if it's limited by Data Validation- then Long or String types might be better choices)

Hope this fixes your problem. Please let me know.

Discuss

Discussion

Thanks, will apply this and see if it works. Appreciate your help.
Gurbir May 13, '21 at 12:06 am
Thanks for the reply Gurbir. If it works for you, kindly  mark my Answer as Selected (for the guidance of others and in accordance with the rules or the Forum)
John_Ru (rep: 6152) May 13, '21 at 1:58 am
Add to Discussion


Answer the Question

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