PDA

View Full Version : Solved: Clear Contents on Workbook Close



gimli
04-21-2010, 11:38 AM
Hey all,

Looking to put some code in to clear the contents of an entire worksheet when the workbook is closed.

EX.


Sheet1.Cells.ClearContents



That should clear the contents..but what extra code is needed to do it on closing the workbook only.

thanks much

Pinokkio
04-21-2010, 12:04 PM
Alt+F11
find ThisWorkbook



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet1.Cells.ClearContents
End Sub

lucas
04-21-2010, 12:40 PM
You will probably wish to save it before it closes too:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet1.Cells.ClearContents
ActiveWorkbook.Save
End Sub

gimli
04-22-2010, 04:08 AM
Ok..heres whats in my workbook page.

1. Locking some cells
2. Preventing anybody but the author saving changes.
3. Trying to add script so when the workbook is closed it clears contents on sheet 2.

Im thinking a problem will occur if the user is not "joe blow" it wont save the workbook after it clears the contents?

any sugestions? Thanks much



Private Sub Workbook_Open()
Sheet1.ScrollArea = "b1:q27"
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
If Environ("username") = "Joe Blow" Then
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
Else
MsgBox "This work book cannot be saved"
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet2.Cells.ClearContents
ThisWorkbook.Save
End Sub

mdmackillop
04-22-2010, 05:18 AM
If the user cannot save changes, is there any need to clear contents?

gimli
04-22-2010, 05:35 AM
Hrmm good point :think: .

Your right...why bother.