Loop Question

WorkMC

Board Regular
Joined
Jul 21, 2005
Messages
88
I'm using the code below to loop through a range of cells, which is currently based on one column. It puts the value in from the loop into cell T1. I need to expand the loop so that columns P & Q are used, the values then need putting in cells T1 and U1.

I have tinkered around with the code but cannot get U1 to show the correct value.

As always any help would be appreciated.


Sub Cells_Loop()
Dim i As Range
Dim LastRowColP As Long

LastRowColP = Sheets("Menu").Range("P65536").End(xlUp).Row
For Each i In Sheets("Menu").Range("P8:P" & LastRowColP) 'or whatever your range is
Sheets("Menu").Range("T1").Value = i.Value
'Call Get_Data
Application.StatusBar = i
Next i
Application.StatusBar = False
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I added 1 line of code.
Although i don't quite understand what this is accomplishing. The loop put all values into T1 & U1 and you will always end up with the last rows data in T1 & U1. Is this what you wanted?
Code:
Sub Cells_Loop()
Dim i As Range
Dim LastRowColP As Long


LastRowColP = Sheets("Menu").Range("P65536").End(xlUp).Row
For Each i In Sheets("Menu").Range("P8:P" & LastRowColP) 'or whatever your range is
Sheets("Menu").Range("T1").Value = i.Value
Sheets("Menu").Range("U1").Value = i.Offset(0, 1).Value
'Call Get_Data
Application.StatusBar = i
Next i
Application.StatusBar = False
End Sub
 
Upvote 0
Do you want last row P data to go into T1, and do you want last row Q data to go into U1? Can you just duplicate your loop again to loop through Q and put the data into U1, although better code may be written to do both loops at the same time.
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,165
Members
448,870
Latest member
max_pedreira

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