need macro (if windows current user... then)

backup69

Active Member
Joined
Jan 20, 2004
Messages
271
Hi

can anyone help me with macro what i can put in workbook.
i need chek current windows user

Private Sub Workbook_Open()

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
like?:
Code:
Private Sub Workbook_Open()
If Application.UserName = "me" Then
'something
else
'something else
End If
End Sub
 
Upvote 0
The following will put the user’s Windows log-on name in sheet1, cell A1

Normal module

Code:
Private Declare Function GetUserName Lib "advapi32.dll" _
    Alias "GetUserNameA" _
   (ByVal lpBuffer As String, _
    nSize As Long) As Long

Public Function UserName() As String
Dim sName As String * 256
Dim cChars As Long
    cChars = 256
    If GetUserName(sName, cChars) Then
        UserName = Left$(sName, cChars - 1)
    End If
End Function
ThisWorkbook module
Code:
Private Sub Workbook_Open()
Dim UserN As String
UserN = UserName()
Sheets(1).Range("A1").Value = UserN
End Sub

Regards,

Mike
 
Upvote 0
Sry but something wrong
Code:
Private Sub Workbook_Open() 
If Application.UserName = "my name" Then 
UserForm1.Show
else 
'something else 
End If 
End Sub
This chek only Name in Excel user but not Windows User

And this code i dont anderstand completely

Code:
Private Declare Function GetUserName Lib "advapi32.dll" _ 
    Alias "My Name" _        '<---------------------this
   (ByVal lpBuffer As String, _ 
    nSize As Long) As Long 

Public Function UserName() As String 
Dim sName As String * 256 
Dim cChars As Long 
    cChars = 256 
    If GetUserName(sName, cChars) Then 
        UserForm1.Show      '<---------------- and this
    End If 
End Function

i dont figure out
i put this in Workbook not to WorkSheet (this not work)
To smart for me :P
 
Upvote 0
Check Mike's code again. You changed it for some reason, and are now confused that it doesn't work. ;)
backup69 said:
Sry but something wrong
{snip}
i dont figure out
i put this in Workbook not to WorkSheet (this not work)
To smart for me :P
 
Upvote 0
backup69: what you need to do is place Mike's following code:

Code:
Private Declare Function GetUserName Lib "advapi32.dll" _ 
    Alias "GetUserNameA" _ 
   (ByVal lpBuffer As String, _ 
    nSize As Long) As Long 

Public Function UserName() As String 
Dim sName As String * 256 
Dim cChars As Long 
    cChars = 256 
    If GetUserName(sName, cChars) Then 
        UserName = Left$(sName, cChars - 1) 
    End If 
End Function

in a regular module, not the worksheet or workbook modules.

then in your workbook module place Mike's other code:

Code:
Private Sub Workbook_Open() 
Dim UserN As String 
UserN = UserName() 
Sheets(1).Range("A1").Value = UserN 
End Sub

and adjust it to your needs. The UserN variable houses the user name, so you could right an If statement of of that like

Code:
If UserN = "Me" then
'something
else
'something else
end if
 
Upvote 0
what do you need it to do with the name once you get it? Ekim's code provides the Windows user logon name.
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top