简体   繁体   English

使用VBA复制和粘贴一系列单元格

[英]Copy and Paste a Range of Cells using VBA

I want to copy a cells in the column C5:C27 and paste them in the range L5:L27, but I keep getting a Run-time error '1004': 我想在列C5:C27中复制一个单元格并将其粘贴在L5:L27范围内,但是我一直收到运行时错误'1004':

This selection is not valid. 此选择无效。 There are several possible reasons: 1. Copy and paste areas cannot overlap unless they are the same size and shape. 有几种可能的原因:1.复制和粘贴区域不能重叠,除非它们具有相同的大小和形状。 2. If you are using the Create from Selection command, the row or column containing the proposed names will not be included in the... 2.如果使用的是“从选择中创建”命令,则包含提议名称的行或列将不包括在...中。

That is all the message box shows. 这就是所有消息框显示的内容。 Every row of the column C5:C27 is only partially filled, could that be the problem here? C5:C27列的每一行仅被部分填充,这可能是这里的问题吗? Is there any other method that I might use to cut and paste in VBA? 我还可以使用其他方法剪切和粘贴VBA吗? Thanks a bunch. 谢谢你

Sub CopyColumn()
    ' Cut and Paste date column
    Worksheets("TankHours").Activate
    Dim TimeCol As Range

    Set TimeCol = Range("C5:C27")
    TimeCol.Cut
    TimeCol.Insert Range("L5:L27")

    End Sub

If you're just copying and pasting I would do like this. 如果您只是复制和粘贴,我会这样做。

Sub CopyColumn()

  Sheets("Sheet1").Range("L5:L27").Value = Sheets("Sheet1").Range("C5:C27").Value

End Sub

One range equals another range. 一个范围等于另一个范围。

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

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