简体   繁体   English

Excel VBA宏:复制范围

[英]Excel VBA Macro: copy range

Dim LastRow As Long
 LastRow = Sheets("Sheet1").Cells(Sheets("sheet1").Rows.Count, "A").End(xlUp).Row
 Sheets("Sheet1").Range("A" & LastRow + 1).FormulaR1C1 = "=WORKDAY(R[-1]C,1)"
 lastrow2 = Sheets("Sheet1").Cells(Sheets("sheet1").Rows.Count, "A").End(xlUp).Row

 'Copy cells
 Sheets("sheet1").Range(??????).Copy
 'Destination
 Sheets("Sheet1").Range("G" & lastrow2 - 3).PasteSpecial xlPasteValues
End Sub

Guys i have problem on copying the range lastrow to lastrow 2. the range is (lastrow -3 on "G" column until lastrow on "O" column). 伙计们,我在将范围lastrow复制到lastrow 2时遇到问题。范围是(“ G”列上的lastrow -3直到“ O”列上的lastrow)。 the expected result is on range (lastrow2 -3 column "G" until lastrow2 on column "O"). 预期结果在范围内(lastrow2 -3列“ G”直到列“ O”上的lastrow2)。 for other information, range is not fully filled. 有关其他信息,范围未完全填满。 there some blank space on the range. 范围上有一些空白。 It means a range that i want to copy. 这意味着我要复制的范围。 But I don't know how should I set the range. 但是我不知道如何设置范围。 Please help me. 请帮我。 I have the excel but I don't allowed to attach image because I have no reputation yet. 我有专业技能,但是因为我还没有声誉,所以我不允许附加图像。

在此处输入图片说明

In the picture you have column F circled, but in your question you mention G. I went with G here. 在图片中,您圈出了F列,但在您的问题中,您提到了G。 I am assuming you lastrow value is correct, which doesn't make sense given your picture, but that is my assumption. 我假设您的lastrow值正确,鉴于您的情况,这没有任何意义,但这是我的假设。 Also I am assuming LastRow tells you what row is the bottom row of your copy range. 我还假设LastRow会告诉您复制范围的最底行是哪一行。

With Sheets("Sheet1")
    Dim rCopy As Range
    Set rCopy = .Range(.Cells(LastRow -2, "G"), .Cells(LastRow, "O"))
    rCopy.Select
    rCopy.Copy
 End With

However, I am not clear on what you are trying to do and I would not use copy and paste if possible. 但是,我不清楚您要做什么,如果可能,我不会使用复制和粘贴。

Edit2: you will also likely have to change your paste line so it uses -2 instead of -3. Edit2:您可能还必须更改粘贴行,以便它使用-2而不是-3。

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

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