Delete all data on a worksheet

S

shiro

Hi all,
I want to delete all dat in the sheet1 when I close the workbook,
but not understand to write VBA code for excel.Is it possible ?
How to write it?

Many thank's.
 
M

Mike H

Hi,

Are you absolutely sure you want to delete everyting? Warning this will
delete everything in Sheet1 and I mean everything so use it with caution and
try it on a backup wotkbook first. Alt + F11 to open VB editor. Double click
'This workbook' and paste the code in.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Sheet1").UsedRange.ClearContents
End Sub

Mike
 
G

Gord Dibben

Right-click on the Excel logo left of "File" on the main menu.

Select "View Code"

Paste this into that module.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook
.Worksheets("Sheet1").Cells.ClearContents
.Save
End With
End Sub


Gord Dibben MS Excel MVP
 
G

Gord Dibben

"logo" should have been "icon"


Gord

Right-click on the Excel logo left of "File" on the main menu.

Select "View Code"

Paste this into that module.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook
.Worksheets("Sheet1").Cells.ClearContents
.Save
End With
End Sub


Gord Dibben MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top