ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping through selected worksheets and delete a range (e.g., A2:D15) (https://www.excelbanter.com/excel-programming/399013-looping-through-selected-worksheets-delete-range-e-g-a2-d15.html)

[email protected]

Looping through selected worksheets and delete a range (e.g., A2:D15)
 
I am trying to loop through the selected sheets and clear the contents
of Range("A2:D15") of all the selected sheets. When I run this code,
it only clears the content of Sheet3. Thank you in advance for your
assistance.
================================================== =========================

Sub ClearSheets

Dim sht As Worksheet

Application.ScreenUpdating = False

Sheets(Array("Sheet3", "Sheet5", "Sheet6", "Sheet8")).Select

For Each sht In ActiveWindow.SelectedSheets

Range("A2:D15").ClearContents

Next sht

Application.ScreenUpdating = True

End Sub
================================================== ===========================


Mike

Looping through selected worksheets and delete a range (e.g., A2:D
 
Maybe something like this
Sub ClearSheets()
Dim x As Object
Sheets(Array("Sheet3", "Sheet5", "Sheet6", "Sheet8")).Select
For Each x In ActiveWindow.SelectedSheets
x.Range("A2:D15").ClearContents
Next x
End Sub

" wrote:

I am trying to loop through the selected sheets and clear the contents
of Range("A2:D15") of all the selected sheets. When I run this code,
it only clears the content of Sheet3. Thank you in advance for your
assistance.
================================================== =========================

Sub ClearSheets

Dim sht As Worksheet

Application.ScreenUpdating = False

Sheets(Array("Sheet3", "Sheet5", "Sheet6", "Sheet8")).Select

For Each sht In ActiveWindow.SelectedSheets

Range("A2:D15").ClearContents

Next sht

Application.ScreenUpdating = True

End Sub
================================================== ===========================




All times are GMT +1. The time now is 04:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com