使用macros插入新行时插入用户名

我想插入'creator'(插入的人)的新行的用户名。 我想要在列G中input的用户名

我目前有以下macros插入行:

Sub Insert_Row() Dim rActive As Range Set rActive = ActiveCell Application.ScreenUpdating = False With Cells(Rows.Count, "H").End(xlUp) .EntireRow.Copy With .Offset(1, 0).EntireRow .PasteSpecial xlPasteFormats .PasteSpecial xlPasteFormulas On Error Resume Next .SpecialCells(xlCellTypeConstants).ClearContents On Error GoTo 0 End With End With rActive.Select Application.CutCopyMode = False Application.ScreenUpdating = True End Sub 

您只需要跟踪插入的行并将列G设置为用户名,如下所示:

 Sub Insert_Row() Dim rActive As Range Dim insertRow As Long Set rActive = ActiveCell Application.ScreenUpdating = False With Cells(Rows.Count, "H").End(xlUp) insertRow = .Row + 1 .EntireRow.Copy With .Offset(1, 0).EntireRow .PasteSpecial xlPasteFormats .PasteSpecial xlPasteFormulas On Error Resume Next .SpecialCells(xlCellTypeConstants).ClearContents On Error GoTo 0 End With End With Cells(insertRow, "G").Value = Environ("Username") rActive.Select Application.CutCopyMode = False Application.ScreenUpdating = True End Sub