Hi, I thank for your interest and I found Step 1 was missed out. I am requesting to please review, if possible, could all 4 steps be met? We will prefer this sheet to be hidden. Password Protection can help manipulation.
Last edited by Aussiebear; 09-04-2011 at 11:07 PM.
Reason: Removed quote to whole of previous post
Please don't make big quotes. I have never seen a case where they are needed.
The code that I posted does what you want and then some. Not all of your steps are really needed. For one thing, when you add time as I did, the date is added as well. Here is an example using the same concepts from the first reply but tailored more to your request. I also added some subs to make it easy to protect and unprotect the log. I added one more field to show open or close.
First, open VBE and set the name of a sheet to LogSheet. This is the codename, not the sheet name shown on the tab. Select View > Project Explorer, select a sheet object, and View > Properties Windows, to set the name property.
In a Module, change the value of pw to suit:
[VBA]Option Explicit
Const pw As String = "ken"
Sub Log(openClose As String)
With LogSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
.Value = Date
.Offset(0, 1).Value = Now
.Offset(0, 2).Value = Environ("username")
.Offset(0, 3).Value = openClose
End With
End Sub
Sub LogProtect()
With LogSheet
.Protect pw, UserInterfaceOnly:=True
.Visible = xlSheetVeryHidden
End With
End Sub
Sub LogUnprotectOpen()
With LogSheet
.Unprotect pw
.Visible = xlSheetVisible
.Activate
.UsedRange.EntireColumn.AutoFit
End With
End Sub[/VBA]
In ThisWorkbook object:
[VBA]Option Explicit
Private Sub Workbook_Open()
LogProtect
Log "Open"
ThisWorkbook.Save
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Log "Close"
ThisWorkbook.Save
End Sub[/VBA]
I must admire and appreciate that design of program was excellent, except:
There was an error:
With Object, must be user defined type. I think correct VBA Program will be this, if we rename Sheet name to LogSheet:-
[vba]
' to be inserted into ThisWorkbook
Option Explicit
Const pw As String = "ken"
Private Sub Workbook_Open()
With Sheets("LogSheet")
.Unprotect pw
.Visible = xlSheetVisible
.Activate
.UsedRange.EntireColumn.AutoFit
End With
Log "Open"
ThisWorkbook.Save
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Log "Close"
With Sheets("LogSheet")
.Protect pw, UserInterfaceOnly:=True
.Visible = xlSheetVeryHidden
End With
ThisWorkbook.Save
End Sub
[/vba]
[vba]
' to be inserted into Module1
Option Explicit
Sub Log(openClose As String)
With Sheets("LogSheet").Range("A" & Rows.Count).End(xlUp).Offset(1)
.Value = Date
.Offset(0, 1).Value = Now
.Offset(0, 2).Value = Environ("username")
.Offset(0, 3).Value = openClose
End With
End Sub
[/vba]
Last edited by sukumar.vb; 09-04-2011 at 04:21 AM.
[vba]
' to be inserted into ThisWorkbook
Option Explicit
Const pw As String = "ken"
Private Sub Workbook_Open()
With Sheets("LogSheet")
.Unprotect pw
.Visible = xlSheetVisible
.Activate
.UsedRange.EntireColumn.AutoFit
End With
Log "Open"
With Sheets("LogSheet")
.Protect pw
.Visible = xlSheetVeryHidden
End With
ThisWorkbook.Save
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Sheets("LogSheet")
.Unprotect pw
.Visible = xlSheetVisible
.Activate
.UsedRange.EntireColumn.AutoFit
End With
Log "Close"
With Sheets("LogSheet")
.Protect pw
.Visible = xlSheetVeryHidden
End With
ThisWorkbook.Save
End Sub