+ Reply to Thread
Results 1 to 11 of 11

keep a log of who accesses a file and how many times

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-11-2014
    Location
    Eugene, Oregon
    MS-Off Ver
    MS EXCEL 2010
    Posts
    210

    keep a log of who accesses a file and how many times

    I have about 12 workbooks and for most people who access those workbooks they are opened read-only because that is the access level that those people have been given to that particular location on our server. I am trying to come up with a way that will keep a log of how many times a workbook is accessed, who accessed it, when they accessed it and what tabs in the workbook they viewed. All 12 workbooks are identical in terms of tabs, layout and formatting but the data is unique to each workbook. I'm not a VBA guru so go easy on me. The data that is logged should never be deleted and would only grow with time. Thanks

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: keep a log of who accesses a file and how many times

    I can see one way to do this: the workbooks will have to write the data to a data repository since the users won't be able to save it in the book itself. Each book will have to have VB code that fires when the book is opened and code on each sheet that fires when it opens. There may be a possibility for "collisions," that is, two people are trying to write to the repository at the same time.

    On every sheet access, the code will open the repository, write the data and close it. This may slow down execution.

    Please provide a sample workbook.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: keep a log of who accesses a file and how many times

    This should get you started - just paste the code into the ThisWorkbook module in VBA
    Option Explicit
    
    Private Sub Workbook_Open()
      LogInfo Environ("Username") & " opened " & Me.Name
      LogInfo Environ("Username") & " has accessed the tab " & ActiveSheet.Name
    End Sub
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
      LogInfo Environ("Username") & " has accessed the tab " & Sh.Name
    End Sub
    
    
    Public Sub LogInfo(LogMessage As String)
      Dim FNum As Long
      
      FNum = FreeFile
      Open ThisWorkbook.Path & "\" & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & "_Log.txt" For Append As #FNum
      Print #FNum, Format(Now, "yyyymmdd_hhmmss") & " " & LogMessage
      Close #FNum
    End Sub
    Last edited by mgs73; 09-19-2018 at 11:21 PM.

  4. #4
    Forum Contributor
    Join Date
    09-11-2014
    Location
    Eugene, Oregon
    MS-Off Ver
    MS EXCEL 2010
    Posts
    210

    Re: keep a log of who accesses a file and how many times

    My Apologies I have been away from work/technology for the better part of a week.

    @DFLAK, I have attached a sample workbook for reference. That is kind of what I was thinking as well but I like your use of the word "Data Repository" that is a good way to define it.
    As for collisions they should be minimal if at all because most of these workbooks have only two people who have access and one of those is a the director and their assistant. The assistant will most likely be the one accessing them most of the time.

    @mgs73, Thank you, I dumped in the code and it kept running but nothing happened. I am sure it is absolutely user error.
    Attached Files Attached Files

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: keep a log of who accesses a file and how many times

    msg73's code looks good to me. The logic of the code is sound. I haven't actually tried it. He's also doing it more efficiently than what I had in mind.

  6. #6
    Forum Contributor
    Join Date
    09-11-2014
    Location
    Eugene, Oregon
    MS-Off Ver
    MS EXCEL 2010
    Posts
    210

    Re: keep a log of who accesses a file and how many times

    dflak, you were right! once I walked myself through the code again, I realized it was working. It was working so well I didn't even realize it was doing anything. mgs73 thank you! You're code is fantastic and certainly runs very efficiently. It is writing the text files and has already been working and capturing who has been accessing the file.

  7. #7
    Forum Contributor
    Join Date
    09-11-2014
    Location
    Eugene, Oregon
    MS-Off Ver
    MS EXCEL 2010
    Posts
    210

    Re: keep a log of who accesses a file and how many times

    mgs73,

    Is it possible to password protect the .txt file once the file is created or maybe within the code itself?

  8. #8
    Forum Contributor
    Join Date
    09-11-2014
    Location
    Eugene, Oregon
    MS-Off Ver
    MS EXCEL 2010
    Posts
    210

    Re: keep a log of who accesses a file and how many times

    The code above works great if you are on a PC but if you are accessing the file from a MAC then the code breaks. upon opening the file the first pop-up window I get says "Additional permissions are required to access the following files:" and the options are "cancel" or "Select" and when I choose "Select" it opens up the location that the excel file lives in but I cannot choose any files as they are all grayed out so than I am forced to hit cancel and that is when I get this error... Run-time error '75': Path/File access error. When I click "debug" the line of code that it highlights is:

    Open ThisWorkbook.Path & "\" & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & "_Log.txt" For Append As #FNum
    (see above for full code)

    I don't know enough about MAC's or VBA to know if there is a solution for this problem or if there is a different code that I can place in a MAC dedicated excel workbook. Currently the excel file is accessed by both PC and MAC users but I could create a separate file for PC and MAC users if someone could help me figure out the code.

  9. #9
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: keep a log of who accesses a file and how many times

    It is your lucky day! I never use a mac, but my wife has one so I gave it a try. It was not easy (for me, anyway) but I think I cracked it!

    First, replace '\' with Application.PathSeparator, i.e.
    Open ThisWorkbook.Path & Application.PathSeparator & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & "_Log.txt" For Append As #FNum
    After I did this, when the Grant Permissions dialog appeared, I was able to press 'Select', and then select the spreadsheet. After that the message came up again, but for the log file. After that - it worked!


    Good luck!
    Last edited by mgs73; 10-24-2018 at 06:19 AM.

  10. #10
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: keep a log of who accesses a file and how many times

    Also, as far as password protection goes, I would say the short answer is no.

    If all you want to do is hide the file from the users you could write the log file to a different directory. They will of course require write access to the directory, so will be able to find the file if they know to look for it. Another option is to make the file hidden:
    SetAttr FilePathName, vbHidden
    The downside is it would hide the file from you too.

    Another idea is to make it Read Only. If you do this, the VBA code would need to make it Read/Write before writing to it, i.e.
    Public Sub LogInfo(LogMessage As String)
      Dim LogFile as String
      Dim FNum As Long
      
      LogFile = ThisWorkbook.Path & Application.PathSeparator & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & "_Log.txt"
      FNum = FreeFile
      SetAttr LogFile, VbNormal
      Open LogFile For Append As #FNum
      Print #FNum, Format(Now, "yyyymmdd_hhmmss") & " " & LogMessage
      Close #FNum
      SetAttr LogFile, VbReadOnly
    End Sub

  11. #11
    Forum Contributor
    Join Date
    09-11-2014
    Location
    Eugene, Oregon
    MS-Off Ver
    MS EXCEL 2010
    Posts
    210

    Re: keep a log of who accesses a file and how many times

    mgs73, You are brilliant! Thank you.

    As for locking the file I figured that wouldn't be an option but I think I might try writing the file to a different directory as you suggested. Thanks again!

+ 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. If a search term can be found 1 time, 2 times 3 times 4 times 5 times
    By excelcandy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2013, 09:57 PM
  2. [SOLVED] VBA-code for opening a file max x times
    By Saturn in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-03-2013, 09:43 AM
  3. Custom Function now accesses a cell due to worksheet size increase in 2010 - How to fix?
    By Culleoka Al in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2013, 03:04 PM
  4. [SOLVED] Macro to open different file at different times
    By mfmalthaf in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-22-2012, 11:01 AM
  5. How to write a formula which accesses the just previous sheet
    By jsingh125 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2012, 07:35 AM
  6. Replies: 1
    Last Post: 02-01-2012, 08:03 AM
  7. Macro that accesses the a web page and copies the content
    By helixman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-04-2008, 01:31 AM
  8. [SOLVED] Tracking who accesses a spreadsheet
    By juamig in forum Excel General
    Replies: 1
    Last Post: 03-08-2006, 01:55 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