我使用以下代码尝试将B4从表单10复制并粘贴到表单6,然后在表单6的b列中输入时间戳。
但是,我在*为的代码行上收到subscript out of range
错误。
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = ThisWorkbook.Sheets(Sheet10Name) ****
Set pasteSheet = ThisWorkbook.Sheets(Sheet6Name)
copySheet.Range("B4").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Range("B" & (ActiveCell.Row)).Select
ActiveCell.Value = Now()
Application.CutCopyMode = False
Application.ScreenUpdating = True
现在显然我没有让now()在sheet6的b列中插入时间戳。
更正最终结果
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = ThisWorkbook.Sheets(Sheet10.Name)
Set pasteSheet = ThisWorkbook.Sheets(Sheet6.Name)
copySheet.Range("B4").Copy
pasteSheet.Cells(pasteSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
pasteSheet.Cells(pasteSheet.Rows.Count, 1).End(xlUp).Offset(0, 1).Value = Now()
Application.CutCopyMode = False
Application.ScreenUpdating = True
答案 0 :(得分:1)
我认为这是你使用(可能是误用)Sheets()
部分的方式。试试这个:
Sub t()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = ThisWorkbook.Sheets(Sheet10.Name)
Set pasteSheet = ThisWorkbook.Sheets(Sheet6.Name)
copySheet.Range("B4").Copy
pasteSheet.Cells(pasteSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
pasteSheet.Range("B" & (ActiveCell.Row)).Select
ActiveCell.Value = Now()
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
另请注意,我已明确在您的Rows.Count
和Range("B" & Activecell...)
部分中提供了工作表,这也应该有所帮助。如果没有它,在切换工作表时可能会出现一些错误。