Macro to clear content of cells

jammoca

Banned
Joined
Nov 6, 2002
Messages
1,100
I have an old macro that used to clear all content within all cells in column B .....

Sub ClearAll()

' ClearAll Macro
' Macro recorded 7/11/2008 by Chris

Columns("B:B").Select
Selection.ClearContents
Range("B2").Select
End Sub

I don't recall what Range("B2").Select did ... can someone explain that to me please?

Also, I need to change the code so that it clears the contents of all cells within column B, column H, column M and cell AC1 .. though I don't want cells B1, H1 and M1 cleared at all.

How might i do this?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Range("B2").Select doesn't 'do' anything except select range B2. This is some of the noise you get when you use the macro recorder to generate macros.

Code:
Sub ClearAll()
 
' ClearAll Macro
' Macro recorded 7/11/2008 by Chris
 
Range("B2:" & "B" & Rows.Count).ClearContents
Range("H2:" & "H" & Rows.Count).ClearContents
Range("M2:" & "M" & Rows.Count).ClearContents
Range("AC1").ClearContents
 
End Sub
 
Last edited:
Upvote 0
Code:
Sub clearem()
Dim e, x
x = Cells.Resize(1)
For Each e In Array(Columns("B"), Columns("H"), Columns("M"))
    e.ClearContents
Next
Cells.Resize(1) = x
Range("AC1").ClearContents
End Sub
Range("B2").Select just selects cell B2, which should stay selected when the code has terminated unless you do another selection in the interim.
 
Upvote 0
You could simply define the desired range :
Code:
Sub ClearRanges()
Range("B2:B65536,H2:H65536,M2:M65536,AC1").ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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