Macro to Clear Data in selected cells

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
Getting Errors when I run the code below. If i different data in the cells from when I recorded the macro I get errors.

Code:
Sub Clear_Sheet()
'
' Clear_Sheet Macro
'


'
    Range("A5:A39,C5:H39,K5:P39,T5:AA39,K1:O1,F1:I1,F2:I2,Z2").Select
    Range("Z2").Activate
    ActiveWindow.SmallScroll Down:=18
    Range( _
        "A5:A39,C5:H39,K5:P39,T5:AA39,K1:O1,F1:I1,F2:I2,Z2,E42:P59,U42:W42,Q42:R59,U42:AA59" _
        ).Select
    Range("U42").Activate
    ActiveWindow.SmallScroll Down:=-33
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=6
    Range("S42:T42").Select
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False
    Range("E42").Select
    ActiveCell.FormulaR1C1 = "5"
    Range("Q42:R42").Select
    ActiveCell.FormulaR1C1 = "5"
    Range("E42").Select
    Selection.ClearContents
    Range("Q42:R42").Select
    Selection.ClearContents
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Aaah the Macro Recorder. We meet again. I think this is what you are trying to do:

Code:
ActiveSheet.Range("A5:A39,C5:H39,K5:P39,T5:AA39,K1:O1,F1:I1,F2:I2,Z2,E42:P59,U42:W42,Q42:R59,U42:AA59" _
        ).Clear
 
Upvote 0
Thanks for the help. I noticed that when I run the macro it deletes the borders and formatting. I need only to delete the data that is in the cell.

Thanks
 
Upvote 0
Best way to learn Vba is to try and read script:

See hear "Clear" clears contents and formatting. Try ClearContents instead like this:
Code:
ActiveSheet.Range("A5:A39,C5:H39,K5:P39,T5:AA39,K1:O1,F1:I1,F2:I2,Z2,E42:P59,U42:W42,Q42:R59,U42:AA59" _
        ).ClearContents
 
Upvote 0
I dont understand. When I run the simple code it says it cant do that to a merged cell. There are lots of merged cells in my worksheet Ive never had this issue before. Also how can I modify the code above to clear sheets 1 thru 26 in the same cells? Thanks for you help
 
Upvote 0
I never deal with merged cells. I do know most all experts on this forum discourage using merged cells.

I think some how we would need to sort out that problem before we attempt to clear contents on 26 sheets.

But this is how you would do it on Sheets 1 to 26 in your Workbook.

Code:
Sub Clear_Me()
Application.ScreenUpdating = False
Dim i As Long
    For i = 1 To 26
        Sheets(i).Range("A5:A39,C5:H39,K5:P39,T5:AA39,K1:O1,F1:I1,F2:I2,Z2,E42:P59,U42:W42,Q42:R59,U42:AA59").ClearContents
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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