Count Cell Indentation in Excel with VBA or a Formula
Question details
Count the indentation for each cell in a worksheet column, whether it comes from cell formatting or leading spaces.

- 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.
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.
Read Excel formatting indent levels with VBA
Use this method when indentation was applied through Excel's cell formatting.
In Excel, select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon settings.
In the VBA editor, select Insert > Module and paste: Function CellIndent(cell As Range) As Long CellIndent = cell.Cells(1, 1).IndentLevel End Function
Return to the worksheet, enter =CellIndent(A2) in B2, and drag the fill handle down for the remaining rows.
Select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) to retain the VBA code.

Count literal leading spaces with an Excel formula
Use this formula when the indentation consists of ordinary spaces typed before the text.
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. Open your spreadsheet: Open WPS Spreadsheet and select File > Open to choose your workbook.
- 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. Fill the results: Select B2 and drag the fill handle down to count leading spaces in the other rows.
- 4. Save your workbook: Select File > Save As and choose an Excel-compatible format such as XLSX.

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.




