Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

On Event VBA code

Posted on 2011-03-08
12
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Accepted Solution

by:
nutsch earned 1000 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 31

Assisted Solution

by:gowflow
gowflow earned 500 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
 
LVL 31

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

715 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