Clear Sheets

Cullen

New Member
Joined
Jan 13, 2009
Messages
47
I need a button to clear everything from sheet 1,2, and 3. I tried:

Private Sub Clearbutton_Click()

Worksheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
Selection.ClearContents

End Sub

Didn't work. Any suggestions? Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Diffy

Well-known Member
Joined
Dec 22, 2006
Messages
512
Umm how about:

Worksheets("Sheet1").Select
Cells.Select
Selection.ClearContents
Worksheets("Sheet2").Select
Cells.Select
Selection.ClearContents
Worksheets("Sheet3").Select
Cells.Select
Selection.ClearContents
 
Upvote 0

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Try

For Each ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
ws.UsedRange.ClearContents
Next ws
 
Upvote 0

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
If you wanted to do it with the selection object then the code would be:
Code:
Private Sub Clearbutton_Click()
    Worksheets("Sheet1").Select
    Worksheets("Sheet1").Cells.Select
    Worksheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
    Selection.ClearContents
End Sub

But I think I prefer a loop here:
Code:
Private Sub Clearbutton_Click()
    Dim wst As Worksheet
    
    For Each wst In Worksheets(Array("Sheet1", "Sheet2", "Sheet3"))
        wst.Cells.ClearContents
    Next wst
    
End Sub
 
Upvote 0

Cullen

New Member
Joined
Jan 13, 2009
Messages
47
This one works great. Thanks!

For Each ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
ws.UsedRange.ClearContents
Next ws

Is there any way that it can be adjusted to strip the formatting/borders?
 
Upvote 0

Forum statistics

Threads
1,191,225
Messages
5,985,360
Members
439,960
Latest member
Musa_dabban

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
Top