VBA macros · Data
VBA macro to convert formulas to values in Excel
Replace the formulas in the selected cells with their current results, keeping number formats. Includes a whole-workbook version and a safety prompt.
Before you send a report, archive a month's figures or delete the source data, you often need to freeze formulas as plain values. The keyboard way is copy, then Paste Special › Values, which is fine for one block but slow for many. This macro converts every formula in the selection to its current result in one step, after asking for confirmation.
Option Explicit
Sub ConvertFormulasToValues()
Dim area As Range
If TypeName(Selection) <> "Range" Then Exit Sub
If MsgBox("Replace the formulas in the selection with their results?" & vbLf & _
"This cannot be undone.", vbOKCancel + vbExclamation, _
"Formulas to values") <> vbOK Then Exit Sub
On Error GoTo Fail
Application.ScreenUpdating = False
' A selection can have several separate areas (Ctrl+click)
For Each area In Selection.Areas
area.Value2 = area.Value2
Next area
Application.ScreenUpdating = True
Exit Sub
Fail:
Application.ScreenUpdating = True
MsgBox "Stopped: " & Err.Description, vbExclamation
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickConvertFormulasToValuesand click Run. - Select the cells to convert before you run it.
- Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
The heart of the macro is a single line: area.Value2 = area.Value2. Reading .Value2 gives the current results of the cells as an array, and writing that array back replaces whatever was there, formulas included, with those results. Cells that already contained plain values are rewritten with the same values, so they do not change.
.Value2 is used instead of .Value on purpose. .Value converts cells formatted as currency into the VBA Currency type, which keeps only four decimal places, so a result like 10.123456 could be cut short. .Value2 returns the raw number (dates as serial numbers), and because the cell formats stay untouched, dates and currencies still look exactly the same afterwards. See Range and Cells: working with cells in VBA.
The loop over Selection.Areas handles selections made with Ctrl+click (Mac: Cmd+click): each separate block is converted on its own, because assigning an array to a multi-area range does not work. The MsgBox with vbOKCancel is the safety catch, since the change cannot be undone.
Change it
To freeze every formula on every sheet of the active workbook, for example before sending a final version, loop over the worksheets and convert each used range:
Sub ConvertAllFormulasInWorkbook()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If Not ws.ProtectContents Then ws.UsedRange.Value2 = ws.UsedRange.Value2
Next ws
Application.ScreenUpdating = True
End SubTo convert only formulas that return errors or only one column, narrow the range: ws.Range("F2:F500").Value2 = ws.Range("F2:F500").Value2. To convert only formula cells and leave typed values alone, use Selection.SpecialCells(xlCellTypeFormulas) and loop over its areas.
What can go wrong
Dynamic array formulas need care: select the whole spill range, including the first cell that holds the formula. If you select only part of it, writing values into the spilled cells blocks the original formula, which shows #SPILL! before it is overwritten. Old-style array formulas entered with Ctrl+Shift+Enter must also be selected completely, otherwise Excel reports You can't change part of an array.
Merged cells inside the selection can cause error 1004; unmerge them first or convert around them. Results that are error values such as #N/A are kept as those errors.
Related: VBA macro to save a backup copy with a timestamp in Excel, VBA macro to trim spaces in selected cells in Excel.