Roni wants to clear everything in a worksheet except for cells which may contain formulas. This task can be completed either manually or through the use of a macro.
If you want to do the clearing manually, you can follow these steps:
Figure 1. The Go To dialog box.
Figure 2. The Go To Special dialog box.
This works great if you only need to clear out the non-formula contents of a worksheet once in a while. If you need to do it more often, then you can simply use the macro recorder to record the above steps. Or, if you prefer, you can create your own macro from scratch, such as the following one:
Sub ClearAllButFormulas() Dim wks As Worksheet 'ignore errors in case there is only formulas On Error Resume Next For Each wks In Worksheets wks.Cells.SpecialCells(xlCellTypeConstants).ClearContents Next On Error GoTo 0 Set wks = Nothing End Sub
This macro is particularly useful if you need to clear out all the non-formula cells in an entire workbook. The reason is because it does the clearing on every worksheet in the entire workbook, without you needing to do the clearing manually.
You should use caution when using this macro. Make sure you really do want to clear out everything except for cells that contain formulas in the worksheet or workbook before you run this macro. It would be a shame to accidentally get rid of the contents of cells that you really needed to keep. If you are a bit squeamish about such a possibility, you might want to use the following version of the macro:
Sub ClearAllButFormulas2() Dim wks As Worksheet Dim sTemp As String Dim iCheck As Integer sTemp = "This macro deletes everything in the current " sTemp = sTemp & "workbook except formulas. Once done, " sTemp = sTemp & "it cannot be undone." & vbCrLf & vbCrLf sTemp = sTemp & "Are you sure you want to continue?" iCheck = MsgBox(sTemp, vbYesNo + vbExclamation, "Warning!") If iCheck = vbYes Then 'ignore errors in case there is only formulas On Error Resume Next For Each wks In Worksheets wks.Cells.SpecialCells(xlCellTypeConstants).ClearContents Next On Error GoTo 0 Else MsgBox "Operation cancelled." End If End Sub
This version displays a message box asking if you are sure you want to do the clearing. The upshot is that you have less of a chance of messing up your workbook by accident.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9097) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Clearing Everything Except Formulas.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Excel has a great (and little known) shortcut for filling a column with information. It comes in very handy when you need ...
Discover MoreWant to show a user, in a cell, what you expect their input to look like? Unfortunately, it cannot be done natively in ...
Discover MoreWhen you are copying a cell from one place to another (perhaps even to a different worksheet), you may not want to copy ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-11-04 09:05:28
R Raghavendran
Thank you for great tips.
2020-09-16 06:51:20
Murray Snudge
Excellent tip - thank you. I've created a workbook for my HMRC tax return and copy the workbook for following year so want to clear previous year data but keep formula. This tip is great!
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments