Clear Contents Macro Help

liquidmettle

New Member
Joined
May 23, 2013
Messages
48
Hi All,

I'm fairly sure this will be an easy one, just can't figure out where I've gone wrong.

I have a sheet named "Update" that I want to put a button on to erase a range (just the contents) on a sheet called "Action Plan".

I do not want to make Action Plan the activesheet, I just want the button to erase it from the Update sheet.

I am trying to use this macro code, but its telling me there is an error:

Sub quarter1()
Sheets("Action Plan").Range("G18:g166").ClearContents
End Sub

Something I'm doing wrong?

Thanks in advance!

-LM
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi Jonmo,

I was receiving the error (which I didn't actually read until you asked-whoops): Run-time error '1004': We can't do that to a merged cell.

The original macro I was trying to modify is:

Sub quarter1()
Range("G21").Select


Do While Selection.Row < 200
Selection.ClearContents
Selection.Offset(3).Select
Loop
Application.Goto (ActiveSheet.Range("A19"))
End Sub

I'm moving the buttons the macros were tied to to the Update sheet, rather than keep them on the Action Plan sheet.

The data I need to be erased are in merged cells, but the macro had no problem erasing them when the buttons were on the Action Plan sheet.

There are four columns I want to be erased (each with their own button/macro).

Columns G, K, O, S. Rather than starting the erasure on g21 (as is in the original macro- I think I changed the row layout since), I need to erase G18:G21, then G23:G26, etc. until G163:G166. The ranges will be the same, just the column letter will differ between the macros.

I want these to run from the Update sheet without jumping to Action Plan (without making it the activesheet in other words).

In each of those 4 cell ranges, there are two merged cells (each spanning two rows, three columns. I don't think the extra columns play into it since its only the first upper left cell that determines content for merged cells). Since there is a spacer row between the part where people would enter data, I guess that might have been causing my error.

Any thoughts?
 
Upvote 0
Well, the error is pretty self explanatory (sorry if that sounds sarcastic, it's not meant to be).

You have to make sure the entire merged range is contained within the range referenced to be cleared.
You can't have half of a named range in the clearcontents command, like
Range("A1:A3").ClearContents if A3:A4 are merged.

Also consider the columns, if it's a multi column merged range..
 
Upvote 0
So for range, would I do something like G18:I21? (That would cover both data cells in that range).

How do I then loop it so I don't have to type out every single range? There is a one row space between all the ranges.
 
Upvote 0
For these ranges specifically
G18:G21, then G23:G26, etc. until G163:G166

you could do
Code:
For i = 18 to 163 Step 5
    Cells(i,"G").Resize(4,1).ClearContents
Next i
 
Upvote 0
Forgive my ignorance (I'm only just starting to learn more about macro code), how would I combine these statements into a working macro then? I tested the macro just based on the G18:i21 range and it did erase what I wanted, just need to account for the other 29 ranges. If you're new code will, how do I combine them?

Truly appreciate the help!

Sub quarter1()
Sheets("Action Plan").Range("G18:i21").ClearContents
End Sub


For i = 18 to 163 Step 5 Cells(i,"G").Resize(4,1).ClearContentsNext i</pre>
 
Upvote 0
We just need to add the sheet reference..and expand to cover columns G to I

Code:
Sub quarter1()
For i = 18 to 163 Step 5
    Sheets("Action Plan").Cells(i,"G").Resize(4,3).ClearContents
Next i

End Sub
 
Upvote 0
The only thing I really changed was to expand the columns.

Do your named ranges cover multiple columns G to I ? Or is it just G ?

Change the 3 back to a 1
Code:
Sub quarter1()
For i = 18 to 163 Step 5
    Sheets("Action Plan").Cells(i,"G").Resize(4,1).ClearContents
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,178
Members
448,871
Latest member
hengshankouniuniu

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