VBA for automatic clearing of unlocked cells upon or re-opening or closing of Work sheet

JulioTheLess

New Member
Joined
Dec 13, 2021
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Guys,

I am wondering if there's a way I can create a my unlocked cells clear upon closing the worksheet or upon re-opening. My goal is that other user may not save the data they input and retain the worksheet on its blank status upon re-opening.

Thanks in advance <3
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Why not just make the workbook read only so any changes can't be saved?
 
Upvote 0
Why not just make the workbook read only so any changes can't be saved?
I was about to utilize it with my staffs as a tool, wherein they need to input information to get a particular answer, yet i'm preventing them to just use the previous data and push them to diligently generate the new information. Also to make recopying of the file useless since it will default to no data content.
 
Upvote 0
Try this
1. Press Alt F11 to open the code editor
2. At left you will see the workbook name, and under it, a subitem Microsoft Excel Objects, and under that, ThisWorkbook.
3. Doubleclick it and a blank code sheet will pop up
4. Paste the code below
5. Close the VBA window and try opening or closing the workbook with data in the unprotected cells

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  ClearUnprotectedCells
End Sub

Private Sub Workbook_Open()
  ClearUnprotectedCells
End Sub

Sub ClearUnprotectedCells()
  Dim c As Range
  For Each c In ActiveSheet.UsedRange
    If c.Locked = False Then c.ClearContents
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top