使用ctrl + shift + down函数又名Selection.End(x1down)进行动态复制和粘贴

时间:2018-08-22 21:05:21

标签: excel vba excel-vba

我正在尝试在两个工作簿之间移动范围(不同长度)。

Windows("Comp1.xlsx").Activate 'Open sheet to pull data from
Range("E2").Select 'Starting point is the same every time
Range(Selection, Selection.End(xlDown)).Select 'Select all data below
Application.CutCopyMode = False
Selection.Copy 'Copy range
Windows("Comps Proto.xlsm").Activate 'Sheet to be pasted into
Range("K12").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False 'Paste data into new sheet

为了对下一个工作簿重复此功能,我需要移走粘贴的选择。我已经尝试了所有方法,包括偏移量和Application.CutCopyMode = False。不起作用

参见下图:第一个粘贴循环以选定的范围结束。我想移动到标有“开始”标记的单元格中。在此处,将以与上述相同的方式粘贴下一个范围,然后冲洗并重复

Picture

2 个答案:

答案 0 :(得分:0)

我要解决的主要问题是使光标单击粘贴范围,以便我可以继续。我发现以下代码很有帮助。这是另一个论坛上的原始帖子:https://superuser.com/questions/342772/how-do-i-move-the-selection-down-one-row-in-excel-2007/342835

Dim ColNumber As Integer

ColNumber = Selection.Column
Range("K" & CStr(ColNumber)).Select 'Click off the pasted values
ActiveCell.End(xlDown).Select 'Equivalent of ctrl+down
ActiveCell.End(xlDown).Select 'Moves down through the range in picture
ActiveCell.Offset(2, 0).Select 'Moves selection to the "Start" point in picture above

答案 1 :(得分:0)

从您的范围位置来看,它的所有边界都有免费单元格。这允许使用方便的CurrentRegion属性。在我的代码中,我假设您有5个这样的块要复制。随时更改要复制到的位置。

Sub MoveCells()
    Dim x%, rng As Range
    Set rng = [E5].CurrentRegion
    '// Copy 5 blocks of cells
    Do
        '// Change target cell to the one you need
        rng.Copy Sheets(2).Cells(1, x + 1)
        '// Here we locate last cell in block of cells, offset two cells down
        '// and select CurrentRegion again
        Set rng = rng(rng.Cells.Count).Offset(2).CurrentRegion
        x = x + 1
    Loop While x <= 5
End Sub