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

1
2
3
4
5
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

1
2
3
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.

Read 48 times
KT_Austin

About:

Welcome to AustinLaptop.Com

Subscribe to our Newsletter
Top
We use cookies to improve our website. By continuing to use this website, you are giving consent to cookies being used. More details…