return beginning and ending of range

RSXchin

Well-known Member
Joined
Oct 23, 2010
Messages
758
Let's say I highlight rows 5 to 11
How do I tell an event to do the same thing to each of these rows?

I don't have a real application for this at this exact moment, but I can tell it would be beneficial.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Maybe something like
Code:
Sub test()
    For Each r In Selection.Rows
        'do your thing here, where r is a range object defined by each row in turn
    Next r
End Sub
 
Upvote 0
if you're being correct, you should dimension the range variable r

Code:
dim r as range
and yes, you can call it anything you like.

there isn't a c in there.
 
Upvote 0
I'm sorry, I've seen other people use
Code:
for every c in range
does that mean cell?
 
Upvote 0
this isn't working
Code:
Sub test()
    Dim r As Range
    For Each r In Selection.Rows
        Cells(r, 1) = "this"
    Next r
End Sub
 
Upvote 0
because r isn't an integer, it's a range object.

If you want to write to the first cell in the range try

Code:
r.cells(1,1)="This"
In your previous example, c can mean cell, but doesn't have to

Code:
dim c as range
for each c in selection
is fundamentally the same as
Code:
dim rhubarb as range
for each rhubarb in selection
but programmers are generally a lazy and cryptic bunch, so short variable names prevail.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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