我正在研究如何创建VBA代码以复制并粘贴带有时间戳的行到存档表。
json_decode
我有“ A-Sheet”,有“ A1”到“ B3” 如何复制到“ B-Sheet”,它将复制到“ A1”到“ B3”,但具有当前时间戳的“ C”?
如果我运行命令,如何使它从下一个可用行(A3)继续复制。
答案 0 :(得分:1)
接下来,将时间戳添加到在K列上复制的每一行中,我还更改了时间戳的格式,以按预期显示年份,从yyy_mm_dd_hh_mm
到yyyy_mm_dd_hh_mm
:
我还建议您使用一种变量来命名它们,以标识它们所保存的数据类型,例如:
Sub Archiving()
Dim LastRow As Long
Dim wsFrom As Worksheet: Set wsFrom = Sheets("asheet")
Dim wsTo As Worksheet: Set wsTo = Sheets("archivesheet")
Dim copyRng As Range
Dim TimeStamp As String
LastRow = wsFrom.Cells(Rows.Count, "E").End(xlUp).Row 'get last row with data on Column E
TimeStamp = Format(CStr(Now), "yyyy_mm_dd_hh_mm") 'format the timestampt
Set copyRng = wsFrom.Range("A2:J" & LastRow) 'set your copy range
wsTo.Range("A2:K" & copyRng.Rows.Count + 1).Insert Shift:=xlDown 'insert rows into sheet before pasting data
copyRng.Copy wsTo.Range("A2") 'paste data into newly inserted empty rows
wsTo.Range("K2:K" & copyRng.Rows.Count + 1).Value = TimeStamp 'add timestampt to copied range
End Sub
更新
要粘贴为值,我将对以下行copyRng.Copy wsTo.Range("A2")
进行如下修改:
copyRng.Copy
wsTo.Range("A2").PasteSpecial xlPasteValues