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.

Windows + Mac

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.

VBA
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 Sub
Freeze the formulas in the selection as values

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick ConvertFormulasToValues and click Run.
  5. Select the cells to convert before you run it.
  6. 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:

VBA
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 Sub
Convert every formula in the workbook (use on a copy!)

To 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

Watch out. Formulas are gone for good after this macro runs. Save a copy first with VBA macro to save a backup copy with a timestamp in Excel, especially before using the whole-workbook version.

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.

Tip. Works on Windows and Mac. On large sheets this is much faster than copy and Paste Special, and it does not touch the clipboard.

Related: VBA macro to save a backup copy with a timestamp in Excel, VBA macro to trim spaces in selected cells in Excel.

More vba macros