how to copy multiple cells from various rows

Falcons88

New Member
Joined
Jun 10, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to copy multiple cells from one sheet and paste them into another, I have code to copy them from a single line which is:

Sheets("Input Sheet").Range("A6:H6").Copy

but I am trying to copy and paste the below highlighted range in a specific order as numbered in the highlighted cells (H18 is the last one to be copied)

1625753449295.png


they are being pasted into the following table called "invoices", the sheet is also called invoices.

1625753544061.png


thanks in advance
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this:

VBA Code:
Sub copycells()
  Dim i As Long, j As Long
  Dim ar1 As Variant, ar2 As Variant
  
  ar1 = Array("B7", "D7", "E7", "A7", "C7", "H18")
  ar2 = Array(1, 2, 3, 4, 5, 7)
  With Sheets("invoices").ListObjects("invoices")
    .ListRows.Add AlwaysInsert:=True
    i = .DataBodyRange.Rows.Count
    For j = 0 To UBound(ar1)
      .DataBodyRange(i, ar2(j)).Value = Sheets("Input Sheet").Range(ar1(j)).Value
    Next
  End With
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub copycells()
  Dim i As Long, j As Long
  Dim ar1 As Variant, ar2 As Variant
 
  ar1 = Array("B7", "D7", "E7", "A7", "C7", "H18")
  ar2 = Array(1, 2, 3, 4, 5, 7)
  With Sheets("invoices").ListObjects("invoices")
    .ListRows.Add AlwaysInsert:=True
    i = .DataBodyRange.Rows.Count
    For j = 0 To UBound(ar1)
      .DataBodyRange(i, ar2(j)).Value = Sheets("Input Sheet").Range(ar1(j)).Value
    Next
  End With
End Sub
Thank you so much @DanteAmor
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top