Variable row selection

diegogda

New Member
Joined
May 11, 2018
Messages
5
Hello:

I want to select an entire row and I have already recorded a macro but the range will vary depending on the file:

Code:
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    Range(Selection, Selection.End(xlDown)).Select
    Rows("73060:1048576").Select
    Selection.ClearContents
    Range("A2").Select
End Sub

How can I set a variable range from the selected rows instead of 73060:1048576?

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe
Code:
   Selection.End(xlDown).Offset(1).Select
   Rows(ActiveCell.Row & ":" & Rows.Count).ClearContents
   Range("A2").Select
 
Upvote 0
I changed it up a bit and now it works:

Code:
    Selection.End(xlDown).Offset(1).Select
    Range(Selection, Selection.End(xlDown)).Select
    Rows(ActiveCell.Row & ":" & Rows.Count).Select
    Selection.ClearContents
    Range("A2").Select
End Sub

Thank you very much!!!
 
Upvote 0
I changed it up a bit and now it works:

Code:
    [COLOR="#0000CD"][B]Selection.End(xlDown).Offset(1).Select
    Range(Selection, Selection.End(xlDown)).Select
    Rows(ActiveCell.Row & ":" & Rows.Count).Select
    Selection.ClearContents[/B][/COLOR]
    Range("A2").Select
End Sub
Whilst it works fine, note that you rarely need to select things in vba to work with them and selecting generally slows your code.
I think the following single line of code could replace all those blue lines without selecting anything
Rich (BB code):
Rows(Selection.End(xlDown).Row + 1 & ":" & Rows.Count).ClearContents
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,852
Messages
6,127,313
Members
449,374
Latest member
analystvar

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