Clear all contents of selected sheets in VBA?

thommo41

Board Regular
Joined
Nov 10, 2006
Messages
142
Hi,

Quite simply, I want to clear the contents of a selection of named sheets.
For example, I might have a book with 5 sheets (named "sheet1", "sheet2" etc) and I want to clear the contents of "Sheet1", "Sheet3" and "Sheet4".
I can code select sheet 1, clear contents, then 3, clear contents etc, but there is a much better, shorter way of coding this.
Thanks in advance
Alan
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try like this

Code:
Sub ClrSheets()
Dim ws As Worksheet
For Each ws In Worksheets(Array("Sheet1", "Sheet3", "Sheet4"))
    ws.UsedRange.ClearContents
Next ws
End Sub
 
Upvote 0
This is a non-looping version.
Code:
Sub test()
    With ThisWorkbook.Sheets(Array("Sheet2", "Sheet3", "Sheet4"))
        .Item(1).Cells.ClearContents
        .FillAcrossSheets .Item(1).Cells, xlFillWithContents
    End With
End Sub

Which prompted a question, if the user selects multiple sheets, how can one find which sheets have been selected?

Nope, I shouldn't hijack this thread, so ... http://www.mrexcel.com/forum/showthread.php?p=2754771#post2754771
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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