Solved

On Event VBA code

Posted on 2011-03-08
12
250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

713 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