How to clear range in selected multiple worksheet

czarpogi

New Member
Joined
Mar 21, 2017
Messages
7
Hi,

I need some help in making this code short and simple. Is there a way to do that? Thanks in advance.

Here's my code:

Code:
Sub ClearData()
With Sheets("NPS")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("QFY")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("MFY")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("W1")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("W2")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("W3")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("W4")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
With Sheets("W5")
    .Range("A2", .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
End Sub
[code/]
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Your wanting to clear contents all cells in Column "A" starting in row (2) of each of these sheets

Is this all the sheets in your workbook?
 
Upvote 0
I have other sheets as well. But I only want to clear the content of the selected worksheet.
 
Upvote 0
Not the entire content. I want to keep row(1) and just clear the date starting from row(2) or from the range("A2"). Thanks.
 
Upvote 0
If you want to keep the header row, as your code suggests, use

Code:
Sub MM1()
Dim sh As Variant
For Each sh In Array("NPS", "QFY", "MFY", "W1", "W2", "W3", "W4", "W5")
Range("A2", Cells(Rows.Count, Columns.Count)).ClearContents
Next sh
End Sub
 
Upvote 0
No MAIT....cant test....don't have excel....so may not work !!

AAHHH....just realised....range needs to be cells


Code:
Sub MM1()
Dim sh As Variant
For Each sh In Array("NPS", "QFY", "MFY", "W1", "W2", "W3", "W4", "W5")
Sheets(sh).Activate
Range(Cells(2, 1), Cells(Rows.Count, Columns.Count)).ClearContents
Next sh
End Sub
 
Upvote 0
Thanks Michael. I just tried the code, however it only clears the first sheet. I don't know if I'm missing something, but I just copied the code you've suggested

If you want to keep the header row, as your code suggests, use

Code:
Sub MM1()
Dim sh As Variant
For Each sh In Array("NPS", "QFY", "MFY", "W1", "W2", "W3", "W4", "W5")
Range("A2", Cells(Rows.Count, Columns.Count)).ClearContents
Next sh
End Sub
 
Upvote 0
Ok i'll try this one. :)

No MAIT....cant test....don't have excel....so may not work !!

AAHHH....just realised....range needs to be cells


Code:
Sub MM1()
Dim sh As Variant
For Each sh In Array("NPS", "QFY", "MFY", "W1", "W2", "W3", "W4", "W5")
Sheets(sh).Activate
Range(Cells(2, 1), Cells(Rows.Count, Columns.Count)).ClearContents
Next sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,556
Messages
6,125,495
Members
449,235
Latest member
Terra0013

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