Hi Kinez,
I am not sure if I really understood, what you need. Still I have modified your macro to get it working, and hopefully help you understand what it does:
Sub Username()
Dim PW As String
PW = "Pass" 'Change for your Password
ActiveSheet.Unprotect PW
'Write Description in column B
ActiveSheet.Range("b1").Value = "MS Office User Name"
ActiveSheet.Range("b2").Value = "Windows User Name"
ActiveSheet.Range("b3").Value = "Computer Name"
'Retrieve the Username (three different options)
ActiveSheet.Range("c1").Value = GetName(1)
ActiveSheet.Range("c2").Value = GetName(2)
ActiveSheet.Range("c3").Value = GetName(3)
ActiveSheet.Protect PW
End Sub
Function GetName(Optional NameType As String) As String
'Function purpose: To return the following names:
'Defaults to MS Office username if no parameter entered
'
'Formula should be entered as =GetName([param])
'
'For Name of Type Enter Text OR Enter #
'MS Office User Name "Office" 1 (or leave blank)
'Windows User Name "Windows" 2
'Computer Name "Computer" 3
'Force application to recalculate when necessary. If this
'function is only called from other VBA procedures, this
'section can be eliminated. (Req'd for cell use)
Application.Volatile
'Set value to Office if no parameter entered
If Len(NameType) = 0 Then NameType = "OFFICE"
'Identify parameter, assign result to GetName, and return
'error if invalid
Select Case UCase(NameType)
Case Is = "OFFICE", "1"
GetName = Application.Username
Exit Function
Case Is = "WINDOWS", "2"
GetName = Environ("UserName")
Exit Function
Case Is = "COMPUTER", "3"
GetName = Environ("ComputerName")
Exit Function
Case Else
GetName = CVErr(xlErrValue)
End Select
End Function
Do you want to use the username to check if a particular user is allowed to open a file? If yes we would simply need to call the function from the Workbook Open Macro. Let me know if that's what you want, and if you need my (or other Forum members) help on this.
Regards
Theo
Bookmarks