Clear Cells, Leave Formulas

JonnieO

New Member
Joined
Jul 18, 2017
Messages
17
I have a spreadsheet that will be used as a template to enter data and then the sheet will be saved as a CSV file with a command button. Once it saves the file it come back to the template and asks if user wants to clear out the data. In order for this to happen and not lose the formulas the sheet must be 1. Unprotected, 2. Cells cleared of data while leaving the formulas, and 3. Sheet should be protected again.

Everything works so far, but it doesn't clear out the data. It will unlock and then lock the cell, so I'm assuming I am missing something with the clear contents portion. The snippet of code where this is happening is below. Any guidance or help would be appreciated.

Code:
With ActiveSheet.Range("A4:O34")
    If MsgBox("Are you sure you want to clear the data?", vbYesNo) = vbYes Then _
    ActiveSheet.Unprotect Password:=""
    ActiveSheet.SpecialCells(xlCellTypeConstants).ClearContents
    ActiveSheet.Protect Password:="", AllowFiltering:=True
End With
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Okay, so I made a subtle change and it seems to work. It may not be the most elegant, but its un-protecting, clearing the data, leaving the formulas, then protecting the sheet again. Silly though, I should have known that I had to tell it what cells to clear.

Code:
With ActiveSheet.Range("A4:O34")
    If MsgBox("Are you sure you want to clear the data?", vbYesNo) = vbYes Then _
    ActiveSheet.Unprotect Password:=""
    ActiveSheet.[B][COLOR="#FF0000"]Range("A4:O34")[/COLOR][/B].SpecialCells(xlCellTypeConstants).ClearContents
    ActiveSheet.Protect Password:="", AllowFiltering:=True
End With
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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