Macro to clear Dataon several worksheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I have a written code to clear data on several worksheets. The data is only cleared on sheet "page1" being the first sheet.

It would be appreciated if you could amend the code so that the data is cleared on all the worksheets

Sub Clear_Data()
For shtName = 1 To Sheets.Count
With Sheets(shtName)
With Range("A:Z")
.ClearContents
End With
End With
Next



End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What about something like this, alter the sheet numbers

Code:
Sub clear1()
Dim i As Integer
For i = 1 To 3
Worksheets(i).Range("A:z").ClearContents
Next i
End Sub
 
Upvote 0
Try this

Sub Clear_Data()

For shtName = 1 To Sheets.Count
Sheets(shtName).Range("A:Z").ClearContents
Next

End Sub

I don't know why your code didn't work, it cleared sheet1 and 3 on my PC but left sheet 2 with data in it ... weird!

Chris
 
Upvote 0
Greetings Howard,

A couple of good examples are already supplied. Just to show why it didn't work...

Rich (BB code):
Sub Clear_Data()
Dim shtName As Long
    
    For shtName = 1 To Sheets.Count
        With Sheets(shtName)
            With .Range("A:Z")
                .ClearContents
            End With
        End With
    Next
End Sub

After the first With, you would need to start Range with a dot, to point it to the range on the correct sheet. Does that make sense?

Mark
 
Upvote 0
Hi Trevor

Thanks for the help, much appreciated
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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