Consulting

Results 1 to 15 of 15

Thread: macro for recording changings

  1. #1
    VBAX Regular
    Joined
    Feb 2007
    Posts
    49
    Location

    macro for recording changings

    hello,

    i was just wonderin if it is possible to record every changings on once excel sheet,
    what happens is i have an excel file which is located in a server at work and 3-4 people enters data in it end of the day if someone makes any mistakes i want to find out which data changed to what..

    i know excel has got track changes function but this file is not a share file.

    thanks in advance

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    How about
    [VBA]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ActiveWorkbook.SaveCopyAs "C:\AAA\Test-" & Application.UserName & ".xls"
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not just log the changes, before and after details with user id.

  4. #4
    VBAX Regular
    Joined
    Feb 2007
    Posts
    49
    Location
    dear xld,
    how can i do that can you be more specific.

    lets say file called tracker and in "O" share drive and user id's as follows
    k.mark & b.dennis & m.iron

    thanks

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add a sheet called "Log"; place this code in ThisWorkbook module

    [vba]Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "Log" Then Exit Sub
    Application.EnableEvents = False
    With Sheets("Log").Cells(Rows.Count, 1).End(xlUp)
    .Offset(1, 0).Value = Application.UserName
    .Offset(1, 1) = Sh.Name
    .Offset(1, 2) = Target.Address
    .Offset(1, 3) = Target.Formula
    End With
    Application.EnableEvents = True
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Use On Error Resume Next after Application.EnableEvents = False in Malcolms code to cater for Event errors generated by clearing a selection of multiple cells, deleting rows etc.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Regular
    Joined
    Feb 2007
    Posts
    49
    Location
    dear all,
    can i also see changing colors deleting information or rows etc,

    pls advice

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Where?
    Are you going to have any form of protection on your workbooks? I'm not sure about recording all events that can occur.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Feb 2007
    Posts
    49
    Location
    yes the actual file is password protected,

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    Add a sheet called "Log"; place this code in ThisWorkbook module

    [vba]Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "Log" Then Exit Sub
    Application.EnableEvents = False
    With Sheets("Log").Cells(Rows.Count, 1).End(xlUp)
    .Offset(1, 0).Value = Application.UserName
    .Offset(1, 1) = Sh.Name
    .Offset(1, 2) = Target.Address
    .Offset(1, 3) = Target.Formula
    End With
    Application.EnableEvents = True
    End Sub
    [/vba]
    I would use

    Environ("UserName")

    rather than

    Application.UserName

    as malcolm did, it is more robust.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    Add a sheet called "Log"; place this code in ThisWorkbook module

    [vba]Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "Log" Then Exit Sub
    Application.EnableEvents = False
    With Sheets("Log").Cells(Rows.Count, 1).End(xlUp)
    .Offset(1, 0).Value = Application.UserName
    .Offset(1, 1) = Sh.Name
    .Offset(1, 2) = Target.Address
    .Offset(1, 3) = Target.Formula
    End With
    Application.EnableEvents = True
    End Sub
    [/vba]
    I would also capture the previous value with the SelectionChange event and record that.

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Incorporating suggestions
    [VBA]Option Explicit

    Dim Previous As String

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Previous = Target.Formula
    End Sub

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "Log" Then Exit Sub
    Application.EnableEvents = False
    On Error Resume Next
    With Sheets("Log").Cells(Rows.Count, 1).End(xlUp)
    .Offset(1, 0).Value = Environ("UserName")
    .Offset(1, 1) = Sh.Name
    .Offset(1, 2) = Target.Address
    .Offset(1, 3) = "'" & Target.Formula
    .Offset(1, 4) = Previous
    Previous = ""
    End With
    Application.EnableEvents = True
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location

    Smile

    Look at that, before the thought is out of my head, Malcolm has coded it

  14. #14
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That's pretty slick. I like the log sheet as record.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #15
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    for anyone who might be interested....this adds the date column which is handy too:
    [VBA].Offset(1, 5) = Now[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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