jbburress
asked on
Capture user name when a new row is inserted in Excel 2003 workbook
Searching previous posts, I found the following code to capture user name in Excel spreadsheet :
Function LastAuthor() As Variant
Application.Volatile
LastAuthor = ActiveWorkbook.BuiltinDocu mentProper ties(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?
Function LastAuthor() As Variant
Application.Volatile
LastAuthor = ActiveWorkbook.BuiltinDocu
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?
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.
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
WhoInsertedRow.xls
ASKER
Hello Tom, thank you, I need to record the user name in a cell of the row that was inserted.
Hi JB. Where will you be recording the username?
Replace:
MsgBox ActiveWorkbook.BuiltinDocu mentProper ties(7) & " inserted a row..."
End Sub
With:
MsgBox Environ("USERNAME") & " inserted a row..."
Replace:
MsgBox ActiveWorkbook.BuiltinDocu
End Sub
With:
MsgBox Environ("USERNAME") & " inserted a row..."
ASKER
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?
"How can I get the name into a cell of the inserted row?"
JB. Which cell?
JB. Which cell?
ASKER
I would like it in column 'S', but the cell number is dependent on where the new row is inserted...
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.
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
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.
:)
"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.
:)
ASKER
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
For example:
01000101
01000102 <- new row added, with some additional fields filled in for information + user name.
01000201
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.
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.
Change the 1 to 19. :)
ASKER
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.
to this: Target.EntireRow.Cells(19)
and then try to insert a row I get "Compile Error: Variable not defined. and the "Target" part of the line is highlighted.
I'm just watching but try this.
Cells(Target.Row,19) = Environ$("USERNAME")
Cells(Target.Row,19) = Environ$("USERNAME")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked great! Thanks for your help and patience. I was missing a couple of things in my code.
Open in new window