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?
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

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,454
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,719
Messages
5,597,733
Members
414,170
Latest member
Mdm

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