Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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.

Clear specified cell contents on workbook open and exit


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.


Related articles:



Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: 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!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    H · 4 days ago
    Hello, for the range how would I enter the code if I want to select multiple range or cells?
  • To post as a guest, your comment is unpublished.
    Sanjay Sharma · 1 months ago
    Dear 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
    • To post as a guest, your comment is unpublished.
      crystal · 8 days ago
      Hi Sanjay Sharma,
      Sorry for the inconvinience. Please apply the below VBA codes and remember to replace the sheet name and the range to your needs. Thank you for your comment.

      VBA code 1:
      Private Sub Workbook_Open()
      Application.EnableEvents = False
      Worksheets("Sheet1").Range("A1:A11").Value = ""
      Application.EnableEvents = True
      End Sub

      VBA code 2:
      Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Worksheets("Sheet1").Range("A1:A11").Value = ""
      End Sub