Selected Rows-VBA

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
699
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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,219,096
Messages
6,146,271
Members
450,682
Latest member
augusto89

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