Selected Rows-VBA

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
696
VBA questions

User selects multiple rows

I need to run certain commands against each row that is selected

I know about ActiveCell.Row so I can run a command against the active cells row, how can I run the command against each row that is selected though

I.e. how do I set the range to each row selected and then run a command against each of those rows?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
here is a sample with a selected range

Code:
Sub LoopSelection()
c = ActiveWindow.RangeSelection.Address
For Each r In Worksheets("Sheet1").Range(c).Cells
r.Value = "aa"
Next r
End Sub
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Or another approach

Code:
Sub test()
Dim r As Long
For r = Selection.Row To Selection.Row + Selection.EntireRow.Count - 1
    Cells(r, 1).Value = r
Next r
End Sub
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,456
Hello cgmojoco,
Here's another quick example. . .
Code:
For Each Rw In Selection.Rows
  MsgBox "The current row is " & Rw.Row
Next Rw

Hopefully one of these examples will point you in the desired direction.
 
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,163,871
Messages
5,834,152
Members
430,261
Latest member
quangtinator

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