Thread: [Solved] ** Using Functions
View Single Post
 
Old 01-31-2011, 03:38 PM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Windows 7 32bit Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

This code goes in the sheet's class module as shown in the attached example.

Code:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
                 "GetUserNameA" (ByVal lpBuffer As String, _
                 nSize As Long) As Long
 
Private Function GetUserName() As String
    Dim sBuff As String * 25
    Dim lBuffLen As Long
 
    lBuffLen = 25
    apiGetUserName sBuff, lBuffLen
    GetUserName = Left(sBuff, lBuffLen - 1)
 
End Function

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim lRow As Long
    
    On Error GoTo ErrorHandler
    
    'did the user change something in column C?
    If Not Intersect(Range("C:C"), Target) Is Nothing Then
        
        lRow = Target.Cells(1).Row
        
        Application.EnableEvents = False
        Cells(lRow, 1).Value = GetUserName
        Cells(lRow, 2).Value = VBA.Now
        
    End If

ErrorExit:
    Application.EnableEvents = True
    Exit Sub
    
ErrorHandler:
    
    Resume ErrorExit
End Sub
Hope that helps...
Attached Files
File Type: zip Example.zip (14.6 KB, 18 views)
Reply With Quote