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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Dermot

Board Regular
Joined
Aug 11, 2006
Messages
199
Office Version
  1. 365
Platform
  1. Windows
Why not just make the workbook read only so any changes can't be saved?
 

JulioTheLess

New Member
Joined
Dec 13, 2021
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Dermot

Board Regular
Joined
Aug 11, 2006
Messages
199
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,176,265
Messages
5,902,239
Members
434,953
Latest member
matthiasarnbert

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
Top