Solved

On Event VBA code

Posted on 2011-03-08
12
240 Views
Last Modified: 2012-06-27
Hi,

I use the below code to measure holw long people stay in a spreadsheet for. The only way i can make this work is by making the WB shared and having it saved on exit.

This is causing problems as people are making a mess of reports and leaving the WB like that.

Can anyone, think of a work around.

So i need to save so that i record what time they left, yet i dont want to save any of their changes, they should be allowed to edit the reports (they need to drill pivots etc) but i cant allow them to save the changes, yet i need to save to monitor!!!

Can anyone think of a way around this?

Thanks
Seamus
Dim R As Long
Dim objSht
Dim t As Double



Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Sheets("Monitor")
    .Range("B64000").End(xlUp).Offset(0, 2) = Now()
End With
ActiveWorkbook.Save
End Sub

Private Sub Workbook_Open()
With Sheets("Monitor")
   .Range("B64000").End(xlUp).Offset(1, 0) = Environ("USERNAME")
   .Range("B64000").End(xlUp).Offset(0, 1) = Now()
End With

With Sheets("Monitor")
 
 R = .Range("B" & Rows.Count).End(xlUp).Row

             
        .Range("E2") = "=VLOOKUP(B2,$K$2:$L$5,2,0)"
       
         
        .Range("F2") = "=INT(C2)"
        
        .Range("G2") = "=MOD(C2,1)"
       
        .Range("H2") = "=MOD(D2,1)"
        
        .Range("I2") = "=H2-G2"
        
        .Range("E2:I2").AutoFill Destination:=.Range("E2:I" & R), Type:=xlFillDefault

Sheets("Screen").Select

For Each objSht In ActiveWorkbook.Sheets
    If objSht.Name <> "Screen" Then
    
      objSht.Visible = xlSheetHidden
    End If

   Next objSht
   
   End With

End Sub

Open in new window

0
Comment
Question by:Seamus2626
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 125 total points
Comment Utility
Don't save the duration data in the workbook - write it out to another file somewhere, such as a network text file.
0
 

Author Comment

by:Seamus2626
Comment Utility
Thats above me Rory! How could i go about that?

Thanks
Seamus
0
 
LVL 24

Expert Comment

by:StephenJR
Comment Utility
Could you have the information stored in another workbook and then not save the original file?
0
 
LVL 39

Accepted Solution

by:
nutsch earned 250 total points
Comment Utility
Like Rory said, something like this (though I can't seem to get it to fire)


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "closed"
MyFile = "c:\temp\logtime.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Print #fnum, Environ("USERNAME") & " left at " & Now()
Write #fnum,
Close #fnum

End Sub

Private Sub Workbook_Open()
Debug.Print "open"
MyFile = "c:\temp\logtime.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Print #fnum, Environ("USERNAME") & " entered at " & Now()
Write #fnum,
Close #fnum
End Sub

Open in new window

0
 

Author Comment

by:Seamus2626
Comment Utility
i see what your getting at, save the environ stuff elsewhere. So on open that gets called and hidden possibly?

Then i dont need the WB to be shared. I must shoot now, i will look at this first thing in the morning!
Thanks
Seamus
0
 
LVL 29

Assisted Solution

by:gowflow
gowflow earned 125 total points
Comment Utility
Well you can try writing on an external file this info instead a sort of a log file
To Read the info in the file:
Open TxtFile For Input As #1

Input #1, UserName , Time
To Write to the File
Open TxtFile For Output As #1
Print #1, Environ("USERNAME"), Now()

Rgds/gowflow
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:gowflow
Comment Utility
Ooops when I wrote my post no one had already answered !! Now I see that my idea was already passed on. Pls understand.
Rgds/gowflow
0
 

Author Comment

by:Seamus2626
Comment Utility
Thanks guys, sorry i could only get back to this now.

WRT to writing to a text file, would this appear int the back ground while a user is in the report, would they see it?

Thanks
Seamus
0
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
No - it would be invisible to them unless you chose to show it.
0
 

Author Comment

by:Seamus2626
Comment Utility
Thanks, i tried the code and get caught on

Open MyFile For Output As fnum

It says

Path/file access error

The location i put it to is
C:\Documents and Settings\travseam\Desktop

What would that error indicate?

Cheers,
Seamus
0
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
Did you specify a file name?
0
 

Author Closing Comment

by:Seamus2626
Comment Utility
Got it, didnt have a file named!!


Cheers,
Seamus
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Splitting out Data 14 26
Copying and pasting pictures from Excel 2 16
Excel 3 20
Excel: formula to determine # to meet goal 5 10
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now