Hi all,
I am trying to use following codes for a add-in utility so that user can enable or disable logging activity for a given workbook.
My problem is to how to add the following code to a new workbook so that "Loginfo" function could be activated. I have tried some application... based commands but it do not seem to be working.
Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
If TypeName(Sh) = "Worksheet" Then
LogInfo Target
End If
End Sub
Is there is any way to add this code to a new workbook using code or any alternative to this activity?
Complete code is mentioned below.
Thanks,
Webbug
Option Explicit
' * * * * *
Private Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
' * * * * *
Sub LogInfo(ByVal Target As Range)
'Variable declaration
Dim FilePath As String
Dim File As Long
Dim Length As Long
Dim Workbook As String
Dim Worksheet As String
Dim Address As String
Dim Value As Variant
Dim Username As String
Dim TimeStamp As Date
Dim Formula As String
Dim Cell As Range
'The log file will contain:
'Workbook full name, Worksheet, Range address,
'New Value, Formula, Username and a time stamp
FilePath = "\\Server\apps\logme.csv"
FilePath = "C:\logme.csv"
'Open the file
File = FreeFile
Open FilePath For Append As #File
Length = LOF(File)
If Length = 0 Then
'Put some headers
Write #File, "Workbook", "Worksheet", "Range", "Value", _
"Formula", "Username", "Date";
End If
'Loop through the cells
For Each Cell In Target.Cells
With Cell
Workbook = .Worksheet.Parent.FullName
Worksheet = .Worksheet.Name
Address = .Address(RowAbsolute:=False, _
ColumnAbsolute:=False)
Value = .Value
If .HasFormula Then
Formula = .Formula
Else
Formula = ""
End If
Username = GetWinUserName()
TimeStamp = Now()
End With
'Write the information
Write #File, Workbook, Worksheet, Address, Value, Formula, _
Username, TimeStamp
Next Cell
Close #File
End Sub
' * * * * *
Function GetWinUserName()
'Variable declaration
Dim strUserName As String
strUserName = String(255, Chr$(0))
GetUserName strUserName, 255
strUserName = Left$(strUserName, InStr(strUserName, _
Chr$(0)) - 1)
GetWinUserName = strUserName
End Function
Bookmarks