Loop On Two Columns

VitesseCarOwner

Board Regular
Joined
Jun 29, 2012
Messages
79
I have two columns of data J2:K1000, I need loop through both columns at the same time and copy the data into cells into E1 and F2, then run through the remainder of the code. The code below I have adapted but cannot get it to work correctly. Can anyone help?



HTML:
Sub Test()
Dim LastRowColJ, LastRowColK As Long
Sheets("Sheet1").Select
LastRowColJ = Range("J65536").End(xlUp).Row
For Each c In Worksheets("Sheet1").Range("J2:J" & LastRowColJ).Cells
Sheets("Sheet1").Range("E1").Value = c
For Each d In Worksheets("Sheet1").Range("K2:K" & LastRowColJ).Cells
Sheets("Sheet1").Range("F2").Value = d
'My other code goes here!
Next
Next
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

rallcorn

Well-known Member
Joined
Nov 11, 2008
Messages
1,027
I think this does what you want:

Code:
Sub Test()
    Sheets("Sheet1").Range("E2:F" & Sheets("Sheet1").Range("J" & Rows.Count).End(xlUp).Row).Value = Sheets("Sheet1").Range("J2:K" & Sheets("Sheet1").Range("J" & Rows.Count).End(xlUp).Row).Value
End Sub
 

rallcorn

Well-known Member
Joined
Nov 11, 2008
Messages
1,027
Code:
Sub Test()
Dim ColJLR As Long
Dim i As Long
ColJLR = Sheets("Sheet1").Range("J" & Rows.Count).End(xlUp).Row
For i = 2 To ColJLR
    Sheets("Sheet1").Range("E" & i & ":F" & i).Value = Sheets("Sheet1").Range("J" & i & ":K" & i).Value
    
    'Add other things you wish to do within row loop
Next i
        
MsgBox "Done!"
    
End Sub
 
Last edited:
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,275
Messages
5,836,337
Members
430,421
Latest member
Natas

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