Macro to retrieve user information

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a need for a function or macro to retrieve the currently logged on
user name. Has anyone done this before or know how to do this? Also, would
the same thing work for Word? I'm running Office 2003.

Thanks!

Randy
 
Jim,

Thanks for your response. After some late night searching, I found this code:

Private Sub Workbook_Open()
Dim rng As Range
Set rng = Sheets("Approvals").Range("A1").End(xlUp).Offset(0, 0)
rng.Value = Environ("UserName")
rng.Offset(0, 1) = Now()
End Sub

It works great but I need some minor modifications. I really do not know
anything about vba and am hoping you can help. The code puts the current
logged on user in one cell and the date and time in the next cell. The date
shows as 8/3/2006 8:22 in the cell and as 8/3/2006 8:22:53 AM in the formula
bar. How can I modify this code to display the entire date format?

Thanks again!

Randy
 
Jim,

Besides the date fix above, I need to know how to undo an action caused by a
control box. I added two check boxes (yes,no) to my sheet and attached the
above macro to each of them. So, when either of the boxes is checked, then
the username and date stamp are displayed. I need to know how to clear this
information if the checkbox becomes unchecked.

Thanks!

Randy
 
Okay, I've come up with this macro that works great! Only need to know how
to do the date formatting.

Private Sub CheckBox1_Click()
If CheckBox1 = True Then
Dim rng As Range
Set rng = Sheets("Approvals").Range("B2").End(xlUp).Offset(1, 0)
rng.Value = Environ("UserName")
rng.Offset(0, 1) = Now()
Else
Set rng = Sheets("Approvals").Range("B2:C2")
rng.ClearContents
End If
End Sub

Private Sub CheckBox2_Click()
If CheckBox2 = True Then
Dim rng As Range
Set rng = Sheets("Approvals").Range("B2").End(xlUp).Offset(3, 0)
rng.Value = Environ("UserName")
rng.Offset(0, 1) = Now()
Else
Set rng = Sheets("Approvals").Range("B4:C4")
rng.ClearContents
End If
End Sub

Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top