?
Solved

Capture user name when a new row is inserted in Excel 2003 workbook

Posted on 2010-01-11
16
Medium Priority
?
242 Views
Last Modified: 2012-05-08
Searching previous posts, I found the following code to capture user name in Excel spreadsheet :

Function LastAuthor() As Variant
Application.Volatile
LastAuthor = ActiveWorkbook.BuiltinDocumentProperties(7)
End Function

And it works great for changes to existing rows, but I need to capture the user name who inserts new rows into the spreadsheet.  How do I go about that?
0
Comment
Question by:jbburress
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
16 Comments
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 26287620
This will get the currently logedon user name

Function UserName() As String
    UserName = Environ("USERNAME")
End Function

Open in new window

0
 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26287654
This code will cover typical row insertions...  Where do you want to record the user who inserted the row???  
The code has comments that explain the need for a named range.

'add a workbook name that will cover the range of expected editing
'and then some.  The name cannot encompass the entire column.
'in this example, I added a name (InsertWatcher) that refers to A1:A10000
Private NumRowsInName As Long

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = Target.EntireRow.Address Then
        NumRowsInName = [InsertWatcher].Rows.Count
    End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If NumRowsInName < [InsertWatcher].Rows.Count Then
        Call RowInserted
    End If
End Sub

Private Sub RowInserted()
    NumRowsInName = [InsertWatcher].Rows.Count
    MsgBox ActiveWorkbook.BuiltinDocumentProperties(7) & " inserted a row..."
End Sub

Open in new window

WhoInsertedRow.xls
0
 
LVL 1

Author Comment

by:jbburress
ID: 26287697
Hello Tom, thank you, I need to record the user name in a cell of the row that was inserted.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26287738
Hi JB.  Where will you be recording the username?  
Replace:
MsgBox ActiveWorkbook.BuiltinDocumentProperties(7) & " inserted a row..."
End Sub
With:
MsgBox Environ("USERNAME") & " inserted a row..."
0
 
LVL 1

Author Comment

by:jbburress
ID: 26288004
I see the results with that change - it is capturing my user name just fine.  I am not so good with VB though.  How can I get the name into a cell of the inserted row?
0
 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26288041
"How can I get the name into a cell of the inserted row?"
JB.  Which cell?
0
 
LVL 1

Author Comment

by:jbburress
ID: 26288065
I would like it in column 'S', but the cell number is dependent on where the new row is inserted...
0
 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26288079
Edit this line:
Target.EntireRow.Cells(1) = Environ$("USERNAME")
The "1" is for column 1.  Replace it with the column number that will store the username.

Option Explicit

'add a workbook name that will cover the range of expected editing
'and then some.  The name cannot encompass the entire column.
'in this example, I added a name (InsertWatcher) that refers to A1:A10000
Private NumRowsInName As Long

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = Target.EntireRow.Address Then
        NumRowsInName = [InsertWatcher].Rows.Count
    End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If NumRowsInName < [InsertWatcher].Rows.Count Then
        Call RowInserted(Target)
    End If
End Sub

Private Sub RowInserted(ByVal Target As Range)
    NumRowsInName = [InsertWatcher].Rows.Count
    Target.EntireRow.Cells(1) = Environ$("USERNAME")
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26288092
Your question is growing!  :)
"but the cell number is dependent on where the new row is inserted"
I have no idea how to determine "where" without more information from you.
:)
0
 
LVL 1

Author Comment

by:jbburress
ID: 26288164
I'm sorry, I maybe am not being clear.  This is a part number sign out spreadsheet.   A user could possibly add a row any where on the sheet - from row 2 through row 17,700 (depending on what part number is being used - it will be added in order).  I need to able to capture the name of any user who adds a row, and I want to keep that name associated with that row from that point on.

For example:
01000101
01000102  <- new row added, with some additional fields filled in for information + user name.
01000201
0
 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26288229
This will place the username in column 'S'.

Target.EntireRow.Cells(1) = Environ$("USERNAME")
"but the cell number is dependent on where the new row is inserted"
If the username will be placed in various columns, you will need to describe the conditions for each scenario.
0
 
LVL 6

Expert Comment

by:TomSchreiner
ID: 26288238
Change the 1 to 19.  :)
0
 
LVL 1

Author Comment

by:jbburress
ID: 26295521
When I changed this:  MsgBox Environ("USERNAME") & " inserted a row..."
to this:  Target.EntireRow.Cells(19) = Environ$("USERNAME")
and then try to insert a row I get "Compile Error:  Variable not defined.  and the "Target" part of the line is highlighted.
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 26296525
I'm just watching but try this.

Cells(Target.Row,19) = Environ$("USERNAME")

0
 
LVL 6

Accepted Solution

by:
TomSchreiner earned 1000 total points
ID: 26299643
The code I posted earlier works ok for me.  Download the attached and see if you are still having issues...

Capture-user-name-when-a-new-row.xls
0
 
LVL 1

Author Closing Comment

by:jbburress
ID: 31675743
Worked great!  Thanks for your help and patience.  I was missing a couple of things in my code.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question