Solved

On Event VBA code

Posted on 2011-03-08
12
242 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
ID: 35071050
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
ID: 35071080
Thats above me Rory! How could i go about that?

Thanks
Seamus
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35071082
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
ID: 35071090
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
ID: 35071137
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
ID: 35071155
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:gowflow
ID: 35072292
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
ID: 35107224
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
ID: 35107247
No - it would be invisible to them unless you chose to show it.
0
 

Author Comment

by:Seamus2626
ID: 35107284
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
ID: 35107299
Did you specify a file name?
0
 

Author Closing Comment

by:Seamus2626
ID: 35107305
Got it, didnt have a file named!!


Cheers,
Seamus
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

863 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

19 Experts available now in Live!

Get 1:1 Help Now