VBA & Macro Problems

Count Cell Indentation in Excel with VBA or a Formula

Camila MilosovichCamila Milosovich Nov 22, 2026 869 views

Question details

Count the indentation for each cell in a worksheet column, whether it comes from cell formatting or leading spaces.

Count Cell Indentation in Excel with VBA or a Formula
Product
Microsoft Excel
Device & OS
not provided
Scenario
A column contains text at different indentation levels, and the user wants a count beside each cell.
Observed behavior
The desired result is the formatting indent level or the number of literal leading spaces for each cell.
Before you start

Determine whether the indentation comes from Excel's Increase Indent formatting or from spaces typed before the text. The VBA method requires a desktop version of Excel that supports macros.

Solution 1Recommended

Read Excel formatting indent levels with VBA

Use this method when indentation was applied through Excel's cell formatting.

1
Open the VBA editor

In Excel, select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon settings.

2
Add the function

In the VBA editor, select Insert > Module and paste: Function CellIndent(cell As Range) As Long CellIndent = cell.Cells(1, 1).IndentLevel End Function

3
Enter and fill the formula

Return to the worksheet, enter =CellIndent(A2) in B2, and drag the fill handle down for the remaining rows.

4
Save the function

Select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) to retain the VBA code.

Read Excel formatting indent levels with VBA
Formatting changes may require recalculation: Changing a cell's indent formatting may not automatically update the result. Force a full workbook recalculation if needed. The function reads formatting levels, not leading spaces.
Count leading spaces in WPS Spreadsheet

Count Leading Spaces in WPS Spreadsheet

Use a worksheet formula in WPS Spreadsheet to count literal leading spaces and fill the results down a column. This method does not read Excel's formatting indent level.

  1. 1. Open your spreadsheet: Open WPS Spreadsheet and select File > Open to choose your workbook.
  2. 2. Enter the formula: In B2, enter =IF(LEN(TRIM(A2))=0,LEN(A2),FIND(LEFT(TRIM(A2),1),A2)-1), replacing A2 if your text is in another cell.
  3. 3. Fill the results: Select B2 and drag the fill handle down to count leading spaces in the other rows.
  4. 4. Save your workbook: Select File > Save As and choose an Excel-compatible format such as XLSX.
Count leading spaces without writing a macroOpen and edit Microsoft Excel XLSX filesFill a formula down a column in a familiar spreadsheet interfaceUse WPS Office for free
microsoft office alternative - wps office

Frequently Asked Questions

Does the VBA function count spaces typed before text?

No. =CellIndent(A2) reads Excel's IndentLevel formatting property. Use the leading-space formula if the spaces are part of the cell's text.

Why didn't the VBA result update after I changed the indent?

A formatting change may not trigger formula recalculation. Force a full workbook recalculation, then check the result again.

Why is LEN(A2)-LEN(TRIM(A2)) not always a leading-space count?

Excel's TRIM also removes trailing spaces and reduces repeated spaces between words. That shorter formula works as a leading-space count only when there are no trailing spaces or repeated internal spaces.

Can I keep the VBA function in an XLSX file?

No. Select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) to retain the function.