Skip i'r prif gynnwys

Sut i gofio neu arbed gwerth cell blaenorol cell wedi'i newid yn Excel?

Fel rheol, wrth ddiweddaru cell gyda chynnwys newydd, bydd y gwerth blaenorol yn cael ei gwmpasu oni bai bod y gweithrediad yn Excel yn cael ei ddadwneud. Fodd bynnag, os ydych chi am gadw'r gwerth blaenorol ar gyfer cymharu â'r un wedi'i ddiweddaru, bydd arbed y gwerth cell blaenorol i gell arall neu i mewn i'r sylw cell yn ddewis da. Bydd y dull yn yr erthygl hon yn eich helpu i'w gyflawni.

Arbedwch werth celloedd blaenorol gyda chod VBA yn Excel


Arbedwch werth celloedd blaenorol gyda chod VBA yn Excel

Gan dybio bod gennych dabl fel y dangosir isod. Os newidiodd unrhyw gell yng ngholofn C, rydych chi am arbed ei gwerth blaenorol i'r gell gyfatebol yng ngholofn G neu arbed sylw yn awtomatig. Gwnewch fel a ganlyn i'w gyflawni.

1. Yn y daflen waith mae'n cynnwys y gwerth y byddwch chi'n ei arbed wrth ddiweddaru, de-gliciwch y tab dalen a dewis Gweld y Cod o'r ddewislen clicio ar y dde. Gweler y screenshot:

2. Yn yr agoriad Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, copïwch y cod VBA isod i mewn i'r ffenestr Cod.

Mae'r cod VBA canlynol yn eich helpu i arbed gwerth celloedd blaenorol colofn benodol i golofn arall.

Cod VBA: Arbedwch werth celloedd blaenorol i mewn i gell golofn arall

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, 7)
        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("C:C"))
    End If
Label1:
    Set xRg = Intersect(Target, Range("C:C"))
    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

Ar gyfer arbed gwerth blaenorol y gell mewn sylw, cymhwyswch y cod VBA isod

Cod VBA: Arbedwch werth celloedd blaenorol yn y sylw

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 xHeader As String
    Dim xCommText As String
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    xHeader = "Previous value :"
    For I = 0 To UBound(xDic.Keys)
        Set xCell = Range(xDic.Keys(I))
        If Not xCell.Comment Is Nothing Then xCell.Comment.Delete
        With xCell
            .AddComment
            .Comment.Visible = False
            .Comment.Text xHeader & vbCrLf & xDic.Items(I)
        End With
    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("C:C"))
    End If
Label1:
    Set xRg = Intersect(Target, Range("C:C"))
    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).Text
        Next
    Next
    Set xChangeRg = Nothing
    Set xRg = Nothing
    Set xDependRg = Nothing
    Application.EnableEvents = True
End Sub

Nodyn: Yn y cod, mae rhif 7 yn nodi'r golofn G y byddwch yn arbed y gell flaenorol iddi, a C: C yw'r golofn y byddwch yn arbed gwerth blaenorol y gell. Newidiwch nhw ar sail eich anghenion.

3. Cliciwch offer > Cyfeiriadau i agor y Cyfeiriadau - VBAProject blwch deialog, gwiriwch y Amser Rhedeg Sgriptio Microsoft blwch, ac yn olaf cliciwch y OK botwm. Gweler y screenshot:

4. Gwasgwch y Alt + Q allweddi i gau'r Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

O hyn ymlaen, pan fydd gwerth y gell yng ngholofn C wedi'i diweddaru, bydd gwerth blaenorol y gell yn cael ei gadw i mewn i gelloedd cyfatebol yng ngholofn G, neu'n arbed sylw fel y dangosodd isod sgrinluniau.

Cadw gwerthoedd celloedd blaenorol mewn celloedd eraill:

Cadw gwerthoedd cell blaenorol mewn sylwadau:

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
Comments (23)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, I'm a newbie of VBA👋

I have a question here 🧐
I pasted the VBA code: Save previous cell value in the comment I my excel but
What if my previous cell is blank then do nothing (No comment) for that particular BLANK cell?
How do I modify the VBA code?
Any expert to provide any solution of this, many thanks👋
This comment was minimized by the moderator on the site
Hi!

Thank you for the function, i would like to know what i have to change to keep all the change.

For exemple if i change two time the value i want te save both last values.

Thank you in advance for the help!
This comment was minimized by the moderator on the site
Hi,
The following VBA code accomplishes this: Track all changes in Column C and store the previous values in successive columns starting from Column G. If Column G is not where you want to start storing these values, adjust the xColumn = 7 line in the code (7 represents Column G, 8 for Column H, and so on).
Hope I can help.

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)
'Updated by extendoffice 20240112
    Dim xCell As Range
    Dim xPrevCell As Range
    Dim xColumn As Long
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    For Each xCell In Target
        If Not xDic.Exists(xCell.Address) Then GoTo NextCell
        If Intersect(xCell, Me.Range("C:C")) Is Nothing Then GoTo NextCell

        ' Find next available column starting from G
        xColumn = 7
        While Me.Cells(xCell.Row, xColumn).Value <> ""
            xColumn = xColumn + 1
        Wend

        ' Save previous value to the next available column
        Set xPrevCell = Me.Cells(xCell.Row, xColumn)
        xPrevCell.Value = xDic(xCell.Address)

NextCell:
    Next xCell

    ' Clear the dictionary and re-enable events
    xDic.RemoveAll
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    On Error GoTo 0
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim cell As Range
    On Error Resume Next
    Application.EnableEvents = False

    ' Reset dictionary and store current values for cells in column C
    xDic.RemoveAll
    For Each cell In Intersect(Target, Me.Range("C:C"))
        If Not cell Is Nothing Then
            xDic.Add cell.Address, cell.Value
        End If
    Next cell

    Application.EnableEvents = True
    On Error GoTo 0
End Sub
This comment was minimized by the moderator on the site
Can any body help in this problem
This comment was minimized by the moderator on the site
saving the previous data when entering manually but not working when data is refreshing from a web site, it is doing nothing
please help
thanks
This comment was minimized by the moderator on the site
Hi Kamal.
This problem is a bit complicated. After trying various methods, I can't deal with it. I am sorry for that.
This comment was minimized by the moderator on the site
only working when entering data manually
but not working when data is refreshing from a website
please help
thanks
This comment was minimized by the moderator on the site
cho e hỏi chút là có cách nào để khi tính toán cộng trừ xong thì nó sẽ lưu lại giá trị khi tính toán xong không ạ
ví dụ:
Giá trị ở cột A = cột B + cột C
Khi tính toán xong cột A sẽ lưu giá trị sau khi đã tính toán xong, lần tiếp theo tính toán thì nó cột A sẽ lấy giá trị hiện tại để tính toán tiếp chứ không lấy giá trị ban đầu ạ
This comment was minimized by the moderator on the site
Hi trung,
The code has been updated. Please give it a try. Thanks for your feedback.
In the following code, the number 5 in this line Set xDCell = Cells(xCell.Row, 5) represents the column E where you will place the previous value. A:A refers to the cells in column A. You need to save the previous values of these cells.

Dim xRg As Range
'Updated by Extendoffice 20220803
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
    Dim X
    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, 5)
        
        xDCell.NumberFormatLocal = xCell.NumberFormatLocal
        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("A:A"))
    End If
Label1:
    Set xRg = Intersect(Target, Range("A:A"))
    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).Text ' xRgArea(J).Formula
        Next
    Next
    Set xChangeRg = Nothing
    Set xRg = Nothing
    Set xDependRg = Nothing
    Application.EnableEvents = True
End Sub
This comment was minimized by the moderator on the site
It is good if you type in.Can you help me to work it in when data is entered by using the value of function from DDE(Dynamic Data Exchange) as well?
This comment was minimized by the moderator on the site
Hi,
Sorry I can't solve this problem. I suggest you post the problem to the forum below to get help from other Excel enthusiasts.
https://www.extendoffice.com/forum/kutools-for-excel.html
This comment was minimized by the moderator on the site
Is there a way to repeat this for all changes? I would like the Comments Box to show all of the previous entries if possible.
This comment was minimized by the moderator on the site
Hi Jennie! Did you manage to solve this issue? I am also trying to collect in a comments box all the new entries, but I am having difficulties to adapt the VBA code to this. Thank you!
This comment was minimized by the moderator on the site
If the cell I want to save is a formula, the G cell will only save the formula, and calculate the value. I need to save the value - not the formula. How can I tell the VBA code, that the value changes although the formula is not changed. Best regards Flemming
This comment was minimized by the moderator on the site
This is for one cell value ,but how do for multiple cell value ,i want 4 cell data store and update like this for example C,D,E,F cell data into G,H,I,J cell respectively ,how can do please help
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations