how to copy multiple cells from various rows

Falcons88

New Member
Joined
Jun 10, 2021
Messages
19
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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,633
Office Version
  1. 2007
Platform
  1. Windows
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
 
Solution

Falcons88

New Member
Joined
Jun 10, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,633
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,141,816
Messages
5,708,752
Members
421,588
Latest member
Wawie

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
Top