Private Sub Workbook_Open()
'Updated by Extendoffice 20190712
Application.EnableEvents = False
Worksheets("test").Range("A1:A11").Value = ""
Application.EnableEvents = True
End Sub
This article is talking about clearing specified cell contents when opening or closing an Excel workbook.
Clear specified cell contents on workbook open and exit
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.