Thanks in advance
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.
Recommended Excel Productivity Tools
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() Application.EnableEvents = False Worksheets("test").Range("A1:A11").Clear Application.EnableEvents = True End Sub
VBA code 2: Clear specified cell contents on workbook exit
Private Sub Workbook_BeforeClose(Cancel As Boolean) Worksheets("test").Range("A1:A11").Clear 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?
Recommended Productivity Tools for Excel
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 27 days agoDear sir/madam, my sheet is protected and only specified cell (for data clearing) is not protected but when i enter the value in the specified & then re-open the excel file then value is cleared by VBA code but the specified cell is protected, i have to un-protect cell by un-protecting the sheet edit the editable range again & again so please fix.
Thanks in advance