Selecting a variable range of cells

wadeer

New Member
Joined
Dec 14, 2017
Messages
7
I have developed a spreadsheet for a golf outing with a maximum number of players set at 28 in Excel 2003. Usually we end up with less than 28 but not always. When I run a macro which copies formulas to the spreadsheet it does so for all 28 rows. I need to select the number of rows within the 28 rows that needs the formulas erased. Cell A31 contains the number of rows that needs to be erased. The columns that contain the formulas are U-AA. Thanks in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Personally I would get the original macro to only fill in the required rows but try the code below on a test sheet.

Code:
Sub eraseRws()
    Dim i As Long
    i = Range("A31").Value
    Intersect(Rows(28).Offset(-(i - 1)).Resize(i), Columns("U:AA"))ClearContents
End Sub
 
Last edited:
Upvote 0
Thanks Mark858. I keyed in as you suggested but received an error message box when I finished with ClearContents. The message stated "Compile Error: Expected: End of Statement". Any suggestions?
 
Upvote 0
Code:
Sub eraseRws()
    Dim i As Long
    i = Range("A31").Value
    Intersect(Rows(28).Offset(-(i - 1)).Resize(i), Columns("U:AA"))[COLOR="#FF0000"][B][SIZE=3].[/SIZE][/B][/COLOR]ClearContents
End Sub

although it should really test for A31 being above 0

Code:
Sub eraseRws()
    Dim i As Long
    i = Range("A31").Value
    If i > 0 Then Intersect(Rows(28).Offset(-(i - 1)).Resize(i), Columns("U:AA")).ClearContents
End Sub
 
Last edited:
Upvote 0
Added the additional code to the beginning of the line and it still errors out. Sorry for being a pest.
 
Upvote 0
Copy and paste the code, don't add it as the correction was in the first code as well and you haven't spotted it.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,052
Messages
6,128,513
Members
449,456
Latest member
SammMcCandless

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