Save the Previous Value of a cell, not the formula

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
657
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I have a Private Sub that retains the Previous Value of a changed cell. It works correctly if I change the cell value manually.
But it copies the Formula in the changed cell. How can I retain the Value not the Formula within a changed cell value?
L:L = value that changes.
N:N(14) = retains previous value
Thank you.

Here is the data:
The Whole Enchilada.xlsm
ABCDEFGHIJKLMN
8 Symbol Sector Type Shares Cost CURRENT MAX Value MIN Value Cost ValueMarket ValueGain/LossG/L %Weight(%)Prev Value
9PRCOXLg GrowthLong1627.723$ 41.631$ 41.73$ 52.00$ 41.73$ 67,763.74$ 67,924.88$ 161.140.24%58.0%0.24%
10RMQHX2xLong45.177$ 426.476$ 446.43$ 550.00$ 446.43$ 20,139.59$ 20,168.37$ 28.780.14%17.2%
11RYTNX2xLong87.584$ 223.065$ 223.59$ 237.16$ 223.59$ 20,139.59$ 19,582.91$ (556.68)-2.76%17.2%
12OAYMXLg ValueLong83.751$ 104.186$ 109.17$ 140.00$ 109.17$ 8,725.64$ 9,143.10$ 417.464.78%7.5%
Schwab
Cell Formulas
RangeFormula
F9:F12F9=IFERROR(VLOOKUP($A9,$A$45:$B$49,2,0),"")
G9:G12G9=IFERROR(IF($F9=0,0,MAX($F9,$G9)),"")
H9:H12H9=IFERROR(IF($H9=0,$F9,MEDIAN($H9,$F9,0)),"")
I9I9=(E9*D9)
J9:J12J9=IFERROR($D9*$F9,"")
K9:K12K9=IFERROR(J9-I9,"")
L9:L12L9=IFERROR((K9/I9),"")
M9:M12M9=IF($I9<>0,I9/$J$16,"")
N9N9=IFERROR((K9/I9),"")
B9:B12B9=IF(COUNTIF(Sectors!$A$3:$AA$50,A9)=1,INDEX(Sectors!$A$2:$AA$2,MAX((Sectors!$A$3:$AA$50=A9)*(COLUMN(Sectors!$A$2:$AA$2)))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A9:E15Expression=MOD(ROW(),2)=1textNO
F9:F15Expression=$F9=$G9textNO
F9:F15,H9:H15Expression=$F9=$H9textNO
A9:A15Expression=$L9<0textNO
A9:A15Expression=$L9>0textNO
M9:O9,M11:O12,M10:N10,M15:O15,M13:N14Expression=$N9="sold"textNO
I9:M15Expression=MOD(ROW(),2)=1textNO
C9:C15Cell Value="put"textNO
C9:C15Cell Value="call"textNO
G9:G15Expression=$F9=$G9textNO
R21:R30,L14:L15,K9:L13Cell Value<0textNO
R21:R30,L14:L15,K9:L13Cell Value>0textNO
B12:B14,B9:B10Expression=#REF!=TODAY()+3textNO
B12:B14,B9:B10Expression=#REF!=TODAY()textNO
B12:B14,B9:B10Expression=#REF!=TODAY()+1textNO
B12:B14,B9:B10Expression=#REF!=TODAY()+2textNO
B12:B14,B9:B10Expression=#REF!="sold"textNO
B10Expression=#REF!=TODAY()+3textNO
B10Expression=#REF!=TODAY()textNO
B10Expression=#REF!=TODAY()+1textNO
B10Expression=#REF!=TODAY()+2textNO
B10Expression=#REF!="sold"textNO
B12:B14,B10Expression=$N51=TODAY()+3textNO
B12:B14,B10Expression=$N51=TODAY()textNO
B12:B14,B10Expression=$N51=TODAY()+1textNO
B12:B14,B10Expression=$N51=TODAY()+2textNO
B12:B14,B10Expression=$N51="sold"textNO
B9:B10Expression=$N52=TODAY()+3textNO
B9:B10Expression=$N52=TODAY()textNO
B9:B10Expression=$N52=TODAY()+1textNO
B9:B10Expression=$N52=TODAY()+2textNO
B9:B10Expression=$N52="sold"textNO
B11Expression=#REF!=TODAY()+3textNO
B11Expression=#REF!=TODAY()textNO
B11Expression=#REF!=TODAY()+1textNO
B11Expression=#REF!=TODAY()+2textNO
B11Expression=#REF!="sold"textNO


Here is the Code:
VBA Code:
Dim xRg As Range
Dim xChangeRg As Range
Dim xDependRg As Range
Dim xDic As New Dictionary
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim I As Long
    Dim xCell As Range
    Dim xDCell As Range
    Dim xHeader As String
    Dim xCommText As String
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    xHeader = "Previous value :"
    x = xDic.Keys
    For I = 0 To UBound(xDic.Keys)
        Set xCell = Range(xDic.Keys(I))
        Set xDCell = Cells(xCell.Row, 14)
        xDCell.Value = ""
        xDCell.Value = xDic.Items(I)
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim I, J As Long
    Dim xRgArea As Range
    On Error GoTo Label1
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    Set xDependRg = Target.Dependents
    If xDependRg Is Nothing Then GoTo Label1
    If Not xDependRg Is Nothing Then
        Set xDependRg = Intersect(xDependRg, Range("L:L"))
    End If
Label1:
    Set xRg = Intersect(Target, Range("L:L"))
    If (Not xRg Is Nothing) And (Not xDependRg Is Nothing) Then
        Set xChangeRg = Union(xRg, xDependRg)
    ElseIf (xRg Is Nothing) And (Not xDependRg Is Nothing) Then
        Set xChangeRg = xDependRg
    ElseIf (Not xRg Is Nothing) And (xDependRg Is Nothing) Then
        Set xChangeRg = xRg
    Else
        Application.EnableEvents = True
        Exit Sub
    End If
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Formula
        Next
    Next
    Set xChangeRg = Nothing
    Set xRg = Nothing
    Set xDependRg = Nothing
    Application.EnableEvents = True
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
In your VBA code change the section of code from:
VBA Code:
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Formula
        Next
    Next

To

VBA Code:
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Value
        Next
    Next

And you are good to Go!
 
Upvote 0
Solution
In your VBA code change the section of code from:
VBA Code:
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Formula
        Next
    Next

To

VBA Code:
    xDic.RemoveAll
    For I = 1 To xChangeRg.Areas.Count
        Set xRgArea = xChangeRg.Areas(I)
        For J = 1 To xRgArea.Count
            xDic.Add xRgArea(J).Address, xRgArea(J).Value
        Next
    Next

And you are good to Go!
THANK YOU for the change.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top