将单元格范围复制到新位置

时间:2019-03-13 22:37:08

标签: excel vba

我要从A1单元格开始复制一系列单元格(动态行数)。下面的代码对我不起作用,因为它没有将整个单元格值范围从当前位置移动到A1。请告知。

Range("E:E").Cut Range("A1")

示例

如果E中的范围是50行,则剪切和移动应从A1开始并在A50处结束。 如果E中的范围是999行,则剪切和移动应从A1开始并在A999处结束。

2 个答案:

答案 0 :(得分:0)

这应该对您有用:

Sub Kopy()
    Dim N As Long
    N = Cells(Rows.Count, "E").End(xlUp).Row
    Range("E1:E" & N).Cut Range("A1")
End Sub

注意:

您不需要为目标指定相同的尺寸。一个单元格即可。

之前:

enter image description here

及之后:

enter image description here

答案 1 :(得分:0)

根据您上面的评论,请参见以下代码:

Sub CutPaste()

    Dim i As Long
    Dim sRow As Long, eRow As Long
    Dim ws As Worksheet

    Set ws = Sheets("Sheet1")

    'set the last row of data
    eRow = ws.Range("E" & ws.Rows.Count).End(xlUp).Row

    'find the start row of data
    For i = 1 To eRow
        If ws.Range("E" & i).Value <> "" Then
            sRow = i
            Exit For
        End If
    Next i

    'now cut and paste
    ws.Range("E" & sRow, "E" & eRow).Cut ws.Range("A1").Paste
    'clear clipboard and object
    Application.CutCopyMode = False
    Set ws = Nothing

End Sub
相关问题