How to clear specified cell contents on open and exit in Excel workbook?
This article is talking about clearing specified cell contents when opening or closing an Excel workbook.
Please do as follows to clear specified cell contents on workbook open and exit.
Firstly, you need to save the workbook which you need to clear specified cell contents on open or exit as an Excel Macro-enabled Workbook.
1. Please click File > Save As > Browse. See screenshot:
2. In the Save As dialog box, please select a folder to save the workbook, enter a new name into the File name box as you need, select Excel Macro-Enabled Workbook from the Sava as type drop-down list, and then click the Save button.
3. In the popping up Microsoft Excel dialog box, please click the OK button as below screenshot shown.
4. Open the Macro-Enabled Workbook you have saved just now, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
5. In the Microsoft Visual Basic for Applications window, double click ThisWorkbook in the left pane, and then copy below VBA code into the Code window.
VBA code 1: Clear specified cell contents on workbook open
Private Sub Workbook_Open() 'Updated by Extendoffice 20190712 Application.EnableEvents = False Worksheets("test").Range("A1:A11").Value = "" Application.EnableEvents = True End Sub
VBA code 2: Clear specified cell contents on workbook exit
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Updated by Extendoffice 20190712 Worksheets("test").Range("A1:A11").Value = "" End Sub
Note: In the above codes, test, and A1:A11 is the worksheet name and cell range you will clear contents from. Please change them as you need.
6. Press the Alt + Q keys to exit the Microsoft Visual Basic for Applications window.
From now on, when opening or closing the workbook, specified cell contents in certain worksheet will be cleared automatically.
- How to clear contents of combo box with VBA code in Excel?
- How to clear contents and formatting at the same time in cells in Excel?
- How to clear contents of named range in Excel?
- How to clear restricted values in cells in Excel?
- How to clear specified cell contents if value of another cell changes in Excel?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!