繁体   English   中英

将活动单元格复制到另一张工作表的 A 列中的最后一个空闲单元格

[英]Copy active cell to last free cell in column A of another sheet

我想将活动单元格复制到另一个名为“评论”的工作表的 A 列中的最后一个空闲单元格。

我试过这段代码:

Sub Macro3()

    'Keyboard Shortcut: Ctrl y
    Dim LastRowMaster As Long

    'Take the cell I'm in
    ActiveCell.Select

    'Copy it
    Selection.Copy

    'Go to the sheet I want to paste into
    Sheets("comments").Select

    'Find the last row in my sheet "comments" and +1
    LastRowMaster = Worksheets("comments").Cells(Worksheets("comments").Rows.Count, 1).End(xlUp).Row + 1

    'Choose the last free cell in column A
    Range("A:").Copy Destination:=ThisWorkbook.Worksheets("comments").Cells(LastRowMaster, "A")

    'Copy my paste
    ActiveSheet.Paste

End Sub

我收到一个错误

“object_global 的方法‘范围’失败

在这一行:

Range("A:").Copy Destination:=ThisWorkbook.Worksheets("comments").Cells(LastRowMaster, "A")

首先,阅读本文,您可以加快和缩短代码。

我认为这可以满足您的要求( A:不是有效的范围参考),但我不建议您将代码基于activecell

Sub Macro3()

Dim LastRowMaster As Long

With Worksheets("comments")                                     'avoid repeating this on every line
    LastRowMaster = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
    ActiveCell.Copy Destination:=.Cells(LastRowMaster, "A")     'copy and paste on the same line
End With

End Sub

那么代码会是这样吗?

Sub Macro3()

Dim LastRowMaster As Long 
    With Worksheets("comments")
    LastRowMaster = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
    ActiveCell.Copy Destination:=.Cells(LastRowMaster, "A").Value = ActiveCell.Value  
End With

End Sub

它出现了一个错误。 我只是错过了一个点/空格或其他东西。 Aaargh,我看不到问题所在。

Range class 的复制方法失败

这应该将您的活动单元格复制到 A 列中下一个空单元格中的“评论”表中:

Sub Macro3()

Dim LastRowMaster As Long 

    With Worksheets("comments")
       LastRowMaster = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
       .Cells(LastRowMaster, "A").Value = ActiveCell.Value  
    End With

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM