Help with simple clearcontents loop function through worksheets

nanook3890

New Member
Joined
May 7, 2014
Messages
21
Hi All,

Can anyone tell me why this code doesn't work? It only clears the active sheet so I know it's something to do with how I use the Worksheets/workbook bit.

All I need to do is clear the contents of the below range on every sheet in the workbook. Can anyone help please? Thanks so much in advance!


Sub Clearcontents()


Dim HolidayDates As Range
Dim wWorksheet As Worksheet




Set HolidayDates = Range("C19:E34")


With Workbooks("Annual Leave 2015.xlsx")

For Each wWorksheet In ActiveWorkbook.Worksheets

HolidayDates.Clearcontents

Next wWorksheet
End With


End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try

Code:
Sub Clcontents()
Dim ws As Worksheet
For Each ws In Workbooks("Annual Leave 2015.xlsx").Worksheets
    ws.Range("C19:E34").ClearContents
Next ws
End Sub
 
Upvote 0
Try

Code:
Sub Clcontents()
Dim ws As Worksheet
For Each ws In Workbooks("Annual Leave 2015.xlsx").Worksheets
    ws.Range("C19:E34").ClearContents
Next ws
End Sub

Amazing - thank you so much! Super concise and works perfectly. :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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