使用宏

时间:2016-09-09 11:57:00

标签: excel vba macros

我想插入“创建者”(插入的人)新行的用户名。我希望在G列中输入用户名。

我目前有以下宏来插入行:

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

1 个答案:

答案 0 :(得分:1)

您只需跟踪插入的行并将列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