+ Reply to Thread
Results 1 to 8 of 8

Audit Trail with VBA!

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-09-2015
    Location
    malta
    MS-Off Ver
    Microsoft 365
    Posts
    387

    Audit Trail with VBA!

    Hi all,

    If it is ok to ask can someone please help me with the below.

    I would like to create a kind of audit trail on my excel which basically will record the cell address of newly inputted data. How I had imagined is as follows:

    There will be 2 sheets:
    - a main sheet 'Home'
    - a second sheet 'Audit Trail'.

    The idea is that:
    Step 1: every time a user types something in Sheet 'Home', a worksheet VBA will record the cell address of the newly inputted data.
    Step 2: the cell address will be pasted in 'Audit Trail' in Column A.

    E.g.That is, if someone types 'hello' in cell A8 of worksheet 'Home'. The macro will record A8 and paste it in 'Audit Trail'.

    P.S Each time data is inserted in 'Home' the cell reference will be pasted in the last blank cell of Column A in 'Audit Trail'

    Not sure if this is possible but would really appreciate a lot your help!!!

    Thanks a lot guys!!
    Keibri

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Audit Trail with VBA!

    Copy the below code and do right click on Home sheet tab and select view code and paste it.

    Close the VBA window (Alt+Q to close VBA window) and return to that sheet and check.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    With ThisWorkbook.Worksheets("Audit Trail")
        Dim iCol As Integer, lRw As Long
        
        iCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        lRw = .Cells(.Rows.Count, iCol).End(xlUp).Row
        
        If lRw = .Rows.Count - 1 Then
            .Cells(lRw + 1, iCol).Value = "--x--"
            iCol = iCol + 1
            lRw = 0
        End If
        
        .Cells(lRw + 1, iCol).Value = Target.Address(0, 0)
    
    End With
        
    End Sub
    Attached Files Attached Files


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Contributor
    Join Date
    02-09-2015
    Location
    malta
    MS-Off Ver
    Microsoft 365
    Posts
    387

    Re: Audit Trail with VBA!

    Cheers Thanks a lot that really worked magic!!! Appreciate a lot man!!

  4. #4
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Audit Trail with VBA!

    Try this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Myr&
        With Sheets("Audit Trail")
            Myr = .Cells(.Rows.Count, 1).End(3).Row
            If Myr = 1 Then
                .Cells(Myr + 1, 1) = Target.Address(0, 0)
            Else
                .Cells(Myr, 1) = Target.Address(0, 0)
            End If
        End With
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    02-09-2015
    Location
    malta
    MS-Off Ver
    Microsoft 365
    Posts
    387

    Re: Audit Trail with VBA!

    Cheers guys!! Also not sure if possible or if it will be difficult.

    The code also records the cell address when the 'delete' button is hit.

    Would it be possible to amend a little bit to distinguish the cell address of those which are entered from those which are deleted?

    Not sure if it is possible or not?

    Thanks a bunch really guys !

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Audit Trail with VBA!

    Replace the given code with the below one.

    Dim sUsedRng As String
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If sUsedRng = Me.UsedRange.Address Then Exit Sub
    
    With ThisWorkbook.Worksheets("Audit Trail")
        Dim iCol As Integer, lRw As Long
        
        iCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        lRw = .Cells(.Rows.Count, iCol).End(xlUp).Row
        
        If lRw = .Rows.Count - 1 Then
            .Cells(lRw + 1, iCol).Value = "--x--"
            iCol = iCol + 1
            lRw = 0
        End If
        
        .Cells(lRw + 1, iCol).Value = Target.Address(0, 0)
    
    End With
        
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        sUsedRng = Me.UsedRange.Address
    End Sub

  7. #7
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Audit Trail with VBA!

    One thing to be aware of - storing the audit information in the workbook can make its size increase dramatically; especially if the workbook is heavily used. You might consider storing the change information in an external CSV file.

  8. #8
    Forum Contributor
    Join Date
    02-09-2015
    Location
    malta
    MS-Off Ver
    Microsoft 365
    Posts
    387

    Re: Audit Trail with VBA!

    Hi again guys

    Sixthsense: Thanks for your help

    Cytop will follow through with your suggestion thanks a lot !

    Thanks

    Keibri
    Last edited by Keibri; 12-27-2016 at 10:57 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Audit trail assistance.
    By sungen99 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2016, 04:22 PM
  2. Turn Audit Trail on and off
    By kati.coy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-22-2016, 11:34 AM
  3. Audit Trail
    By Moggzzz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-29-2016, 06:48 AM
  4. Audit Trail
    By jenziepie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2013, 01:56 PM
  5. Excel Audit Trail?
    By jvest0648 in forum Excel General
    Replies: 1
    Last Post: 12-17-2011, 02:57 PM
  6. Track Changes - Audit Trail
    By ChemistB in forum Excel General
    Replies: 2
    Last Post: 10-27-2006, 02:16 PM
  7. [SOLVED] Audit Trail
    By Pendelfin in forum Excel General
    Replies: 1
    Last Post: 01-23-2006, 11:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1