Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
To reproduce this issue, follow these steps, as appropriate for the version of Excel that you are running.
Excel 2007
- Open a new workbook in Excel.
- Click the Microsoft Office Button, click Save As, click Excel Macro-Enabled Workbook (*.xlsm) in the Save as type box, type a file name in the File name box, and then click Save.
- Type some text in cell A1, and then press ENTER.
- Click the Developer tab. If the Developer tab is not displayed, follow these steps:
- Click the Microsoft Office Button, and then click Excel Options.
- Click Popular.
- Click to select the Show Developer tab in the Ribbon check box.
- Click OK to close the Excel Options dialog box.
- Click Visual Basic in the Code group to start the Visual Basic Editor.
- In the Project - VBAProject pane, double-click ThisWorkbook.
- Follow these steps:
- In the Object box, click Workbook.
- In the Procedure box, click BeforeSave.
- Add the following macro code to the BeforeSave event:
Range("a1").ClearContents
- Under the macro code that you typed in step 7c, type the following macro code:
Public Sub Test()
ThisWorkbook.Save
End Sub
- In the workbook, click the Microsoft Office Button, and then click Save.
The text in cell A1 is cleared. - Click Cancel to close the Save As dialog box.
- Type some text in cell A1, and then press ENTER.
- On the Developer tab, click Macros in the Code group.
- Click the ThisWorkbook.Test macro, and then click Run.
The text in cell A1 is not cleared when the workbook is saved.
Excel 2003 and earlier versions of Excel
- Open a new workbook in Excel.
- Type some text in cell A1, and then press ENTER.
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- In the Project - VBAProject pane, double-click ThisWorkbook.
- Follow these steps:
- In the Object box, click Workbook.
- In the Procedure box, click BeforeSave.
- Add the following macro code to the BeforeSave event:
Range("a1").ClearContents
- Under the macro code that you typed in step 5c, type the following macro code:
Public Sub Test()
ThisWorkbook.Save
End Sub
- In the workbook, click Save on the File menu.
The text in cell A1 is cleared. - Click Cancel to close the Save As dialog box.
- Type some text in cell A1, and then press ENTER.
- On the Tools menu, point to Macro, and then click Macros.
- Click the ThisWorkbook.Test macro, and then click Run.
The text in cell A1 is not cleared when the workbook is saved.