Namig: drugi jeziki so prevedeni v Google. Lahko obiščete English različico te povezave.
Vpiši se
x
or
x
x
Registracija
x

or

Kako zapomniti ali shraniti prejšnjo vrednost celice spremenjene celice v Excelu?

Običajno se pri posodabljanju celice z novo vsebino prekrije prejšnja vrednost, razen če ne razveljavite operacije v Excelu. Vendar, če želite ohraniti prejšnjo vrednost za primerjavo s posodobljenim, bo shranjevanje prejšnje vrednosti v drugo celico ali v celični komentar dobra izbira. Metoda v tem članku vam bo pomagala doseči.

Shrani prejšnjo vrednost celice s kodo VBA v Excelu


Shrani prejšnjo vrednost celice s kodo VBA v Excelu


Predvidevam, da imate prikazano tabelo pod prikazom slike. Če se je spremenila katera koli celica v stolpcu C, jo želite shraniti v ustrezno celico stolpca G ali samodejno shraniti v komentar. Prosimo, da to storite, kot sledi.

1. Na delovnem listu vsebuje vrednost, ki jo boste shranili pri posodabljanju, z desno miškino tipko kliknite zavihek stanja in izberite Ogled kode iz menija z desnim klikom. Prikaz slike:

2. V odprtju Microsoft Visual Basic za aplikacije okno, kopirajte spodnjo kodo VBA v okno Code.

Naslednja VBA koda vam pomaga shraniti prejšnjo vrednost celice določenega stolpca v drug stolpec.

VBA koda: shranite prejšnjo vrednost celice v drugo celico stolpca

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

Če želite shraniti prejšnjo vrednost celice v komentarju, uporabite spodnjo kodo VBA

VBA koda: shranite prejšnjo vrednost celice v komentar

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

Opombe: V kodi številka 7 označuje stolpec G, ki jo shranite v prejšnjo celico, in C: C je stolpec, ki ga shranite v prejšnjo vrednost celice. Prosimo, da jih spremenite glede na vaše potrebe.

3. klik Orodja > Reference da odprete Reference - VBAProjekt pogovorno okno, preverite Microsoft Scripting Runtime polje in končno kliknite na OK gumb. Prikaz slike:

4. Pritisnite druga + Q tipke za zapiranje Microsoft Visual Basic za aplikacije okno.

Od zdaj naprej, ko bo posodobljena vrednost celice v stolpcu C, bo prejšnja vrednost celice shranjena v ustrezne celice v stolpcu G ali pa bo shranjena v komentarju, kot je prikazano na sliki spodaj.

Shrani prejšnjo vrednost v drugo celico:

Shrani prejšnjo vrednost v komentarju:


Priporočena orodja za produktivnost za Excel

zavihek kte 201905

Kutools za Excel vam pomaga, da vedno končate delo pred časom in izstopite iz množice

  • Več kot zmogljive napredne funkcije 300, zasnovane za 1500 delovne scenarije, ki povečujejo produktivnost z 70%, vam dajejo več časa za skrb za družino in uživanje v življenju.
  • Ne potrebujete več pomnilniških formul in VBA kod, od zdaj naprej pa dajate svojim možganom počitek.
  • Postanite strokovnjak za Excel v minutah 3, zapletene in ponavljajoče se operacije lahko opravite v nekaj sekundah,
  • Vsak dan zmanjšajte število operacij tipkovnice in miške, zdaj se poslovite od poklicnih bolezni.
  • 110,000 visoko učinkovite ljudi in 300 + svetovno priznanih podjetij izbiro.
  • Brezplačna preizkusna različica 60 dneva. 60-dnevno jamstvo vračila denarja. 2 let brezplačne nadgradnje in podpore.

Prinaša kartično brskanje in urejanje za Microsoft Office, veliko močnejši od zavihkov brskalnika

  • Office Tab je namenjen za Word, Excel, PowerPoint in druge Office aplikacije: Založnik, Dostop, Visio in Projekt.
  • Odprite in ustvarite več dokumentov v novih zavihkih istega okna in ne v novih oknih.
  • Z 50% poveča vašo produktivnost in vsak dan zmanjša na stotine klikov z miško!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    OscarLopez · 1 months ago
    I was thinking about in a sentence "If/else" but I´m new using VBA, so if you have another post that could me help me, please share with me, and again thank you ! keep sharing the knowledge
  • To post as a guest, your comment is unpublished.
    OscarLopez · 1 months ago
    Hi ! I just wanted to know if it is possible register multiples changes to the cell, I mean, if I put data in the cell C2 and then I change that data for other information, the previous data pass to the cell G2 (like in this post), but If I change the value one more time in cell C2, the second change that I made pass to the cell H2 (for example) and now I registered the information of the 3 movements that I realized, and do it almost 5 times more (save the previous cell value 5 times). If you could help me I would appreciate so much because here in your post it´s the only place that I found where solve my problem partialy. Thanks for sharing this content!!!!

  • To post as a guest, your comment is unpublished.
    Gustavo Vier · 10 months ago
    i need something like this, but only in specific cells (ex.: G12 to show in H23 the old value)
    • To post as a guest, your comment is unpublished.
      Gustavo Vier · 10 months ago
      And other ... I need this run when a cell change by a result (EX.: A1 + B1 = C1... if I change A or B value, the script does not work - nothing happens in G cell)