简体   繁体   中英

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). the expected result is on range (lastrow2 -3 column "G" until lastrow2 on column "O"). 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. I am assuming you lastrow value is correct, which doesn't make sense given your picture, but that is my assumption. Also I am assuming LastRow tells you what row is the bottom row of your copy range.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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