Doorgaan naar hoofdcontent

Excel: Keeping Track Of Changes in Your Data Using VBA

Today I created an Excel workbook that keeps track of changes in your data, on a hidden worksheet called history.

This is the VBA script I used, the code must be placed in ThisWorkbook,

Option Explicit
Public oldValue As Variant

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim intRows As Integer
    Dim rngCell As Range
    intRows = Sheets("history").UsedRange.Rows.Count + 1
    
    'MsgBox Target.Count
    'to prevent changes on the history sheet are recorder as well
    If Sh.Name <> "history" Then
        intRows = Sheets("history").UsedRange.Rows.Count + 1

        If Target.Count = 1 Then
            Sheets("history").Cells(intRows, 1).Offset(0, 0).Value = Target.Address
            Sheets("history").Cells(intRows, 1).Offset(0, 1).Value = oldValue
            Sheets("history").Cells(intRows, 1).Offset(0, 2).Value = Target.Value
            Sheets("history").Cells(intRows, 1).Offset(0, 3).Value = Now()
            Sheets("history").Cells(intRows, 1).Offset(0, 4).Value = Sh.Name
            Sheets("history").Cells(intRows, 1).Offset(0, 5).Value = Application.UserName
        Else
            For Each rngCell In Target.Cells
                intRows = Sheets("history").UsedRange.Rows.Count + 1

                Sheets("history").Cells(intRows, 1).Offset(0, 0).Value = rngCell.Address
                Sheets("history").Cells(intRows, 1).Offset(0, 1).Value = oldValue
                Sheets("history").Cells(intRows, 1).Offset(0, 2).Value = rngCell.Value
                Sheets("history").Cells(intRows, 1).Offset(0, 3).Value = Now()
                Sheets("history").Cells(intRows, 1).Offset(0, 4).Value = Sh.Name
                Sheets("history").Cells(intRows, 1).Offset(0, 5).Value = Application.UserName
            Next
        End If
    End If
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    oldValue = Target.Value
End Sub

You need an additional sheet called history to store the changes. In the North American Excel version, history is a reserved name. So you have to think of a different one. You can hide this sheet, of course. The sheet will look like this:


+Brian Canes tipped me to shorten the Workbook_SheetChange code to:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim intRows As Integer
    Dim rngCell As Range
    intRows = Sheets("history").UsedRange.Rows.Count + 1
    
    'MsgBox Target.Count
    'to prevent changes on the history sheet are recorder as well
    If Sh.Name <> "history" Then
        intRows = Sheets("history").UsedRange.Rows.Count + 1

        If Target.Count = 1 Then
            Sheets("History").Cells(intRows, 1).Resize(1, 6) = _
            Array(Target.Address, oldValue, Target.Value, Now(), Sh.Name, Application.UserName)
        Else
            For Each rngCell In Target.Cells
                intRows = Sheets("history").UsedRange.Rows.Count + 1
                Sheets("History").Cells(intRows, 1).Resize(1, 6) = _
                Array(Target.Address, oldValue, Target.Value, Now(), Sh.Name, Application.UserName)
            Next
        End If
    End If
End Sub

Reacties

Populaire posts van deze blog

Excel: VBA script om wachtwoord te verwijderen

Af en toe krijg ik een vraag om een wachtwoord van een Excel blad te halen. Doodsimpel met VBA. Hier een script dat ik gebruik: Sub WachtwoordCrack()     Dim a As Integer, b As Integer, c As Integer, d As Integer, _     e As Integer, f As Integer, g As Integer, h As Integer, _  I As Integer, j As Integer, k, m As Integer     Dim begin As Date, eind As Date     Dim duur As String     Dim objSheet As Worksheet     begin = TimeValue(Time)     On Error Resume Next     For Each objSheet In Application.Worksheets         For a = 65 To 66: For b = 65 To 66: For c = 65 To 66             For d = 65 To 66: For e = 65 To 66: For f = 65 To 66                 For g = 65 To 66: For h = 65 To 66: For I = 65 To 66                     For j = 65 To 66: For k = 65 To...

Excel: gegevenslabel alleen bij de laatste waarde in een grafiek

Creatief met Corona Bij het maken van een grafiek over de voortschrijdende Corona cijfers in Nederland liep ik tegen een probleem aan. Als je bij een reeks gegevens in een grafiek de gegevenslabels aan zet, krijg je die labels bij elke waarde. Ik wilde dit label alleen bij de laatste waarde tonen. Nu kun je natuurlijk alle andere gegevenslabels afzonderlijk wissen, maar dat is een hoop werk. Op zoek naar een andere manier dus. De cijfers datum aantal 1-3-2020 3 2-3-2020 8 3-3-2020 5 4-3-2020 15 5-3-2020 44 6-3-2020 46 7-3-2020 60 8-3-2020 76 9-3-2020 57 10-3-2020 61 11-3-2020 121 12-3-2020 111 13-3-2020 190 14-3-2020 155 15-3-2020 176 16-3-2020 278 17-3-2020 292 18-3-2020 250 19-3-2020 20-3-2020 21-3-2020 22-3-2020 23-3-2020 24-3-2020 25-3-2020 ...

Vrouwen van het Friese verzet. Gevecht op vele fronten.

  Al toen mijn vorige boek, de biografie van Piet Oberman zijn voltooiing naderde, rees bij mij het idee onderzoek te doen naar de bijdrage van vrouwen aan het Friese verzet. Hoe groot was hun bijdrage eigenlijk geweest? En wie waren deze vrouwen precies? Eenmaal aan de slag werden me van alle kanten namen aangereikt. Of ik aan Wieke Bosch gedacht had? Nee, nooit van gehoord zelfs. Of Bouwina Meindertsma? Ja, die naam was ik eerder tegen-gekomen. Zelf ging ik ook op onderzoek uit en zo onstond gaande-weg een lijst met tweeëndertig namen van vrouwen die in de oorlog betrokken waren bij het verzet tegen de Duitse overheerser. Voor een deel afkomstig uit Friesland, maar voor een groot deel ook uit de rest van Nederland. Uit Groningen, Utrecht en Noord- en Zuid-Holland. En zelfs een jonge Joodse vrouw uit Duitsland. Ze moesten in de Tweede Wereldoorlog het hoofd bieden aan een wrede bezetter. Maar het boek gaat niet alleen over de oorlog.  De vrouwen moesten ook nog opboksen ...