Clear Contents of Variable Range(s)

rrmando

Board Regular
Joined
Dec 23, 2004
Messages
212
Hello everyone. Pleasure to be here. I have the following macro to clear contents of ranges:

Code:
Range("A3:R2223,V3:AF2223,AH3:AI2223,AM3:AM2223,AP3:AP2223").Select
    Selection.ClearContents
    Range("A3").Select

The problem is that I'm a rookie and the range is variable, so I have to manually change the range. So the last row (2223) gets changed to whatever it happens to be for that month.

My question (finally), is there a way to generate an input box requesting the range end, row 3 will ALWAYS be the first row, and then clear contents based on that? The column references will never change, only the last row is variable. Thank you for your help. Have a great day!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Do you really need to ask for the last row?

Could you not use code to get it?
Code:
LastRow = Range("A" & Rows.Count).End(xlUp).Row

Range("A3:R" & LastRow &",V3:AF" & LastRow  &",AH3:AI" & LastRow &",AM3:AM" & LastRow & ",AP3:AP" & LastRow).ClearContents
 
Upvote 0
I would do this slightly differently than Norie simply because IMO the code below is easier to understand and maintain.
Code:
Sub useIntersect()
    Dim AllCols As Range, AllRows As Range
    
    With ActiveSheet
    Set AllCols = .Range("A:R,V:AF,AH:AI,AM:AM,AP:AP")
    Set AllRows = .Range("3:" & (.UsedRange.Row + .UsedRange.Rows.Count - 1))
    Application.Intersect(AllCols, AllRows).ClearContents
        End With
    End Sub
Of course, if we remove variables that are used only once, we get
Code:
Sub useIntersect2()
    With ActiveSheet
    Application.Intersect(.Range("A:R,V:AF,AH:AI,AM:AM,AP:AP"), _
            .Range("3:" & (.UsedRange.Row + .UsedRange.Rows.Count - 1))) _
        .ClearContents
        End With
    End Sub

Hello everyone. Pleasure to be here. I have the following macro to clear contents of ranges:

Code:
Range("A3:R2223,V3:AF2223,AH3:AI2223,AM3:AM2223,AP3:AP2223").Select
    Selection.ClearContents
    Range("A3").Select

The problem is that I'm a rookie and the range is variable, so I have to manually change the range. So the last row (2223) gets changed to whatever it happens to be for that month.

My question (finally), is there a way to generate an input box requesting the range end, row 3 will ALWAYS be the first row, and then clear contents based on that? The column references will never change, only the last row is variable. Thank you for your help. Have a great day!
 
Upvote 0
tushar

I was considering another approach perhaps using Union, Resize, Offset etc

But IMO the code I posted is easier to understand than that approach, especially considering the OP has said they're a VBA rookie.

But that's my opinion, and there is, as the say, many ways to skin a cat.:)
 
Upvote 0
I appreciate the responses, however let me throw a little wrench I forgot to mention. The reason I need to specify the last row is because I have sum, subtotal, etc., etc. formulas below that last row.

In this instance, the formulas run across row 2224, but the formulas do not reside in every cell on that row, they will always reside only on columns R:T and AB:AL.

Having said that, can code be used to locate the last row of data based on one of these columns, and then proceed with selecting the range and clearing content? Can I adapt one of your fine examples to work with this?

Thanks again for your answers. I truly want to thank all of you for sharing your Excel knowledge with us newbies.
 
Upvote 0

Forum statistics

Threads
1,203,127
Messages
6,053,663
Members
444,676
Latest member
locapoca

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