Loop to copy/paste next row using VBA

bpg_23

New Member
Joined
Jul 7, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have the following code from a recorded macro and I'm trying to copy/paste down approximately 1000 rows.

Range("J3:K3").Select
Selection.Copy
Range("N1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Range("L3").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

My code needs to change the "J3:K3" range and "L3" range to the next row down the list of approximately 1000 rows. Apologize for the basic question, I'm very new to VBA and have spent the last hour or so searching to no avail.

Thanks!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi bpg,
You can copy a range including the first and last row you wish to copy... So, copying J3:K1000 would copy the entire range. When you paste, it would then paste the way you want... Something like the following should get you close... change the 1000 to whatever number you want...


VBA Code:
Range("J3:K1000").Select

Selection.Copy

Range("N1:N1000").Select

Selection.PasteSpecial Paste:=xlPasteValues

Range("L3").Select

Selection.Copy

Selection.PasteSpecial Paste:=xlPasteValues
 
Upvote 0
Hi bpg,
You can copy a range including the first and last row you wish to copy... So, copying J3:K1000 would copy the entire range. When you paste, it would then paste the way you want... Something like the following should get you close... change the 1000 to whatever number you want...


VBA Code:
Range("J3:K1000").Select

Selection.Copy

Range("N1:N1000").Select

Selection.PasteSpecial Paste:=xlPasteValues

Range("L3").Select

Selection.Copy

Selection.PasteSpecial Paste:=xlPasteValues
Thanks for the tip. That didn't quite do what I needed it to do because a formula is being driven from cells N1:O1.

The values from J3:K3 are being pasted into N1:O1 which is driving a formula that is changing L3. I'm pasting values in L3 so it does not change as the macro works through the rows.
 
Upvote 0
Quite frankly, I find your question a little confusing. It might be more helpful if we could see examples of what your data looks like, and what you want to happen.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Quite frankly, I find your question a little confusing. It might be more helpful if we could see examples of what your data looks like, and what you want to happen.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

First time using this so hopefully it works.

Interstate_Exit-US-AL-Alabama.csv
ABCDEFGHIJKLM
133.47505-81.9954
2LatLongDISTStoreLatLong
332.86002-85.1732188.629AUGUSTA_0010533.47505-81.99543.740044
433.67579-85.3426193.1756BOWLINGGREEN_0060136.97918-86.45413.740044
533.34295-87.0273290.3372MARTINEZ_7070133.53931-82.08163.740044
633.99587-84.5589151.6194GREENVILLE_0010934.83888-82.35613.740044
734.71405-85.5536220.8388CLARKESVILLE_0030134.60186-83.51783.740044
834.99227-86.8791297.9714AUGUSTA_7070233.42319-82.02023.740044
934.98587-85.2012210.777GREENVILLE_0016434.88826-82.4063.740044
1032.36722-86.3219262.3207AIKEN_0016633.52701-81.72223.740044
1132.85449-85.1786189.029BOWLINGGREEN_0020136.95087-86.43683.740044
Interstate_Exit-US-AL-Alabama
 
Upvote 0
The previous mini-sheet is not correct -- this one is accurate for the VBA originally posted --

Interstate_Exit-US-AL-Alabama.csv
ABCDEFGHIJKLMNO
133.47505-81.9954
2LatLongDISTStoreLatLong
332.86002-85.1732188.629AUGUSTA_0010533.47505-81.99543.740044
433.67579-85.3426193.1756BOWLINGGREEN_0060136.97918-86.45413.740044
533.34295-87.0273290.3372MARTINEZ_7070133.53931-82.08163.740044
633.99587-84.5589151.6194GREENVILLE_0010934.83888-82.35613.740044
734.71405-85.5536220.8388CLARKESVILLE_0030134.60186-83.51783.740044
834.99227-86.8791297.9714AUGUSTA_7070233.42319-82.02023.740044
Interstate_Exit-US-AL-Alabama
 
Upvote 0
OK, so is that what your data looks like initially?
Then what do you want to happen after that?
What should it look like when it is done?
 
Upvote 0
OK, so is that what your data looks like initially?
Then what do you want to happen after that?
What should it look like when it is done?
The lat/long from Cols J/K should be pasted into N1:O1. This will update the distance in Col E.
Col L uses formulas to find the MIN value from Col E. The end of the loop will have the MIN value from Col E pasted as a value in L3.

Sorry for the confusion and thanks for your help.
 
Upvote 0
So, you just want to loop through columns J and K and paste the values into N1:O1?
Then something like this should get you started.
VBA Code:
Sub MyCopy()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column J with data
    lr = Cells(Rows.Count, "J").End(xlUp).Row
    
'   Loop through all rows and paste values from J and K into N1:O1
    For r = 3 To lr
        Range(Cells(r, "J"), Cells(r, "K")).Copy Range("N1")
    Next r
        
    Application.ScreenUpdating = True
    
End Sub
I am sure that you will have other stuff to add to the loop, otherwise it just keeps going through all the rows and pasting over the values in N1:O1 without doing anything else.
 
Upvote 0
So, you just want to loop through columns J and K and paste the values into N1:O1?
Then something like this should get you started.
VBA Code:
Sub MyCopy()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column J with data
    lr = Cells(Rows.Count, "J").End(xlUp).Row
   
'   Loop through all rows and paste values from J and K into N1:O1
    For r = 3 To lr
        Range(Cells(r, "J"), Cells(r, "K")).Copy Range("N1")
    Next r
       
    Application.ScreenUpdating = True
   
End Sub
I am sure that you will have other stuff to add to the loop, otherwise it just keeps going through all the rows and pasting over the values in N1:O1 without doing anything else.
Thanks Joe4, I’ll give this a shot and will let you know how it works.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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