VBA - Help with LOOPs - copying range to another worksheet's next available row

ESoda

New Member
Joined
May 15, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am looking for some assistance making the code below run for all selected cells in rows 27-67. The code below works for the first row; it copies contents from the selected cells to the next available row on a different worksheet.How can I make it loop through rows 27-67, copying cells from the same columns, to the next available row in the other worksheet? I am a VBA novice and suspect there are a number of different ways to approach this.

Here's the code:

Sub CopyToDATASheet()

Dim src As Worksheet
Dim dst As Worksheet
Dim LastRow As Long

Application.ScreenUpdating = False

' Set source and destination sheets
Set src = Sheets("PRODUCTION")
Set dst = Sheets("table")


' Find next available row on destination sheet

LastRow = Worksheets("table").Range("A" & Rows.Count).End(xlUp).Row + 1

' Populate values on destination sheet
dst.Cells(LastRow, "A") = src.Range("B27")
dst.Cells(LastRow, "B") = src.Range("A27")
dst.Cells(LastRow, "C") = src.Range("F27")
dst.Cells(LastRow, "D") = src.Range("G27")


Application.ScreenUpdating = True

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
.
How about a slight change to your macro ??

Edit your source range as required.



VBA Code:
Option Explicit

Sub CopyToDATASheet()

Dim src As Worksheet
Dim dst As Worksheet
Dim LastRow As Long

Application.ScreenUpdating = False

' Set source and destination sheets
Set src = Sheets("PRODUCTION")
Set dst = Sheets("table")


' Find next available row on destination sheet

LastRow = Worksheets("table").Range("A" & Rows.Count).End(xlUp).Row + 1

' Populate values on destination sheet
src.Range("B2:B20").Copy Destination:=dst.Cells(LastRow, "A")
src.Range("A2:A20").Copy Destination:=dst.Cells(LastRow, "B")
src.Range("F2:F20").Copy Destination:=dst.Cells(LastRow, "C")
src.Range("G2:G20").Copy Destination:=dst.Cells(LastRow, "D")


Application.ScreenUpdating = True

End Sub
 
Upvote 0
Option Explicit Sub CopyToDATASheet() Dim src As Worksheet Dim dst As Worksheet Dim LastRow As Long Application.ScreenUpdating = False ' Set source and destination sheets Set src = Sheets("PRODUCTION") Set dst = Sheets("table") ' Find next available row on destination sheet LastRow = Worksheets("table").Range("A" & Rows.Count).End(xlUp).Row + 1 ' Populate values on destination sheet src.Range("B2:B20").Copy Destination:=dst.Cells(LastRow, "A") src.Range("A2:A20").Copy Destination:=dst.Cells(LastRow, "B") src.Range("F2:F20").Copy Destination:=dst.Cells(LastRow, "C") src.Range("G2:G20").Copy Destination:=dst.Cells(LastRow, "D") Application.ScreenUpdating = True End Sub

Thanks for the quick help. How would I change it to copy the values in the cells rather than the formulas?
 
Upvote 0
See if this does it ...

VBA Code:
Option Explicit

Sub CopyToDATASheet()

Dim src As Worksheet
Dim dst As Worksheet
Dim LastRow As Long

Application.ScreenUpdating = False

' Set source and destination sheets
Set src = Sheets("PRODUCTION")
Set dst = Sheets("table")


' Find next available row on destination sheet

LastRow = Worksheets("table").Range("A" & Rows.Count).End(xlUp).Row + 1

' Populate values on destination sheet
src.Range("B2:B20").Copy
dst.Cells(LastRow, "A").PasteSpecial Paste:=xlPasteValues

src.Range("A2:A20").Copy
dst.Cells(LastRow, "B").PasteSpecial Paste:=xlPasteValues

src.Range("F2:F20").Copy
dst.Cells(LastRow, "C").PasteSpecial Paste:=xlPasteValues

src.Range("G2:G20").Copy
dst.Cells(LastRow, "D").PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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