[英]VBA Excel - Insert in New Row
I have code that works to insert a username and timestamp into a spreadsheet. 我有可以将用户名和时间戳插入电子表格的代码。 However, it currently overwrites the existing records. 但是,它当前会覆盖现有记录。 I'm struggling with adding logic to the code so when the spreadsheet is opened, the new username/timestamp is appended to the next blank row. 我正在努力向代码中添加逻辑,因此在打开电子表格时,新的用户名/时间戳会附加到下一个空白行。 Any assistance is greatly appreciated! 非常感谢您的协助!
Private iNextRow As Long
Const HIDDEN_SHEET As String = "Sheet3"
Private Sub Workbook_Open()
With Worksheets(HIDDEN_SHEET)
.Range("A1").Value = Environ("UserName")
.Range("B1").Value = Format(Date + Time, "dd mmm yyyy hh:mm:ss")
End With
iNextRow = 2
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error GoTo wb_exit
Application.EnableEvents = False
If Sh.Name <> HIDDEN_SHEET Then
With Worksheets(HIDDEN_SHEET)
.Range("A" & iNextRow).Value = Environ("UserName")
.Range("B" & iNextRow).Value = Format(Date + Time, "dd mmm yyyyhh: mm: ss ")
End With
End If
wb_exit:
Application.EnableEvents = True
End Sub
找到要放置新数据range.EntireRow.Insert range = range.Offset(-1, 0)
然后填写数据
Try this: 尝试这个:
Dim nextRow as integer
nextRow = Range("A1").End(xlDown).Offset(1,0).Row
Cells(nextRow,1)=(Environ$("Username"))
Cells(nextRow,2)=VBA.Now()
Could not refrain to re-write slightly asp8811 excellent solution: 无法克制asp8811的出色解决方案:
With Sheet3.Range("A1").End(xlDown)
.Offset(1,0) = environ$("UserName")
.Offset(1,1) = Now
End with
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.