简体   繁体   中英

Copy & Paste multiple cells & rows in VBA excel

I have to copy multiple rows in VBA excel repeatedly.

Does anyone knows some shorter solution how to make this?

My current solution looks like this:

'Select row with sun altitude values
Range("A11:A277").Select
Selection.Copy
Range("EE11:EE277").PasteSpecial xlPasteValues
'Select row 11
Range("B11:EB11").Copy
Range("EF11").PasteSpecial xlPasteValues
Range("EF37").PasteSpecial xlPasteValues
Range("EF41").PasteSpecial xlPasteValues
Range("EF71").PasteSpecial xlPasteValues
Range("EF101").PasteSpecial xlPasteValues
Range("EF131").PasteSpecial xlPasteValues
Range("EF161").PasteSpecial xlPasteValues
Range("EF191").PasteSpecial xlPasteValues
Range("EF221").PasteSpecial xlPasteValues
Range("EF251").PasteSpecial xlPasteValues
'Select column B and paste in every next columns
Range("B11:B277").Select
Selection.Copy
Range("EF11").PasteSpecial xlPasteValues
Range("FU11").PasteSpecial xlPasteValues
Range("FX11").PasteSpecial xlPasteValues
Range("HM11").PasteSpecial xlPasteValues
Range("HQ11").PasteSpecial xlPasteValues
Range("JF11").PasteSpecial xlPasteValues

In general I have to copy 10 the same rows and at least 6 the same columns. The exact distance between every row is 30 interspersed in every 26 , as per above and in the picture attached. I have seen previous solution for copy multiple cells in VBA provided, however it refers more for issues with external workbooks.

Thanks & regards, 在此处输入图片说明

By combining the Areas into one Range with Commas. So this:

'Select row 11
Range("B11:EB11").Copy
Range("EF11").PasteSpecial xlPasteValues
Range("EF37").PasteSpecial xlPasteValues
Range("EF41").PasteSpecial xlPasteValues
Range("EF71").PasteSpecial xlPasteValues
Range("EF101").PasteSpecial xlPasteValues
Range("EF131").PasteSpecial xlPasteValues
Range("EF161").PasteSpecial xlPasteValues
Range("EF191").PasteSpecial xlPasteValues
Range("EF221").PasteSpecial xlPasteValues
Range("EF251").PasteSpecial xlPasteValues

becomes

'Select row 11
Range("B11:EB11").Copy
Range("EF11,EF37,EF41,EF71,EF101,EF131,EF161,EF191,EF221,EF251").PasteSpecial xlPasteValues

If the Rows are pasted at a regular interval, you could use a Loop, either to pate, or to build a Range

For i = 1 to 17 Step 4 'Paste every 4 rows
    Cells(136,i).PasteSpecial xlPasteValues 'Paste in column EF, a.k.a. column 136
Next i

or

Set RngTmp = Cells(136,1)
For i = 5 to 17 Step 4 'Paste every 4 rows
    Set RngTmp = Union(RngTmp, Cells(136,i)) 'Add the next cell to the range
Next i
RngTmp.PasteSpecial xlPasteValues 'Paste to all cells in the range

You can do it in one line. For example, your copy/paste of row 11 can be done:

Range("B11:EB11").Copy
Range("EF11, EF37, EF41, EF71, <all the others in your list> , EF251").PasteSpecial xlPasteValues

That is, you list all the cell destinations separated by commas, in quotes.

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