In Excel VBA, use a cell’s IndentLevel property to read or set its indent: for example, Range("A1").IndentLevel = 2 indents the cell’s contents by two levels. The property applies to cell alignment, not to the worksheet row or column, and Excel allows indent levels from 0 through 15.
What IndentLevel means
Indentation adds space between a cell’s edge and its displayed contents. It is an alignment setting, similar to choosing Increase Indent on Excel’s ribbon. It does not insert spaces into the cell value, alter the underlying text or number, or change the cell’s position in the worksheet grid.
In VBA, IndentLevel belongs to the Range object. A range can refer to one cell or multiple cells. When assigning one level to a multi-cell range, Excel applies that setting to the cells in the range. When reading a range containing cells with different settings, it may be more useful to inspect each cell individually rather than assume the result represents every cell.
Set an indent with VBA
Assign a whole-number level to the property. Level 0 removes indentation; higher numbers add progressively more indent. This example sets a few levels on a worksheet named “Summary.”
Sub SetCellIndents()
' Set indentation on selected cells on the Summary worksheet.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Summary")
ws.Range("A1").IndentLevel = 0
ws.Range("A2").IndentLevel = 1
ws.Range("A3").IndentLevel = 2
End Sub
Replace the worksheet name and addresses with those in your workbook. If the sheet name does not exist, VBA raises an error. A range reference such as ws.Range("A2:A10").IndentLevel = 1 applies level 1 to all cells from A2 through A10.
Before running a macro, save a copy of the workbook. Macro changes generally cannot be reversed with Ctrl+Z, so a backup is the simplest way to recover if the macro changes the wrong range.
Read, change, or clear an indent
To retrieve a cell’s current level, assign its IndentLevel value to a variable or display it in the Immediate window. You can also change a cell relative to its existing setting, while ensuring the level remains within Excel’s supported range.
Sub AdjustCellIndent()
' Increase A1 by one level, without exceeding 15.
Dim cell As Range
Dim currentLevel As Long
Set cell = ThisWorkbook.Worksheets("Summary").Range("A1")
currentLevel = cell.IndentLevel
If currentLevel < 15 Then
cell.IndentLevel = currentLevel + 1
End If
End Sub
To remove indentation, set the level to 0. This is usually clearer than subtracting one repeatedly, especially when the starting level is unknown. To inspect a value while debugging, use Debug.Print cell.IndentLevel in a procedure and open the Immediate window in the VBA editor with Ctrl+G on Windows.
Indent levels and horizontal alignment
Indentation is most visible when a cell has left, right, or distributed horizontal alignment. Excel’s Increase Indent command can also adjust alignment as needed for the chosen cell. If the visual result does not look as expected, inspect the cell’s HorizontalAlignment setting as well as its indent level; the property controls only the number of indent steps, not the overall alignment mode.
For example, indentation is commonly used to show hierarchy in a report: a main category at level 0, a subcategory at level 1, and a detail item at level 2. It is a visual convention, not a data relationship. Sorting, filtering, formulas, and PivotTable grouping do not interpret indentation as a parent-child structure. If the hierarchy matters for analysis, store category levels in separate columns or use an appropriate outline or grouping feature.
Common issues and practical limits
- Invalid level: Excel supports integer levels from 0 to 15. Check a calculated level before assigning it so a macro does not try to go below 0 or above 15.
- Wrong worksheet: An unqualified reference such as
Range("A1")acts on the active sheet. Qualify the range with a worksheet variable, as in the examples, to avoid changing a different sheet. - Unexpected appearance: Column width, horizontal alignment, text direction, and number format can affect how a cell looks. IndentLevel does not widen a column or wrap text automatically.
- Text spaces mistaken for indentation: Leading spaces are part of a text value;
IndentLevelis formatting. Choose the property when you want adjustable presentation without modifying the stored value. - Mixed formatting: If a range contains different indent levels, read individual cells when your code needs each exact value. Assigning to the whole range intentionally replaces the cells’ existing levels with one setting.
Use VBA or Excel’s ribbon?
For a handful of cells, selecting them and using Home > Alignment > Increase Indent or Decrease Indent is usually faster than writing a macro. The buttons can be clicked repeatedly to change the level. Their location may appear as icons in the Alignment group, depending on the ribbon layout.
VBA is useful when indentation must be applied consistently across many rows, repeated after data refreshes, or controlled by a procedure. It also makes it straightforward to set exact levels rather than count button clicks. On Excel for Mac, VBA is available in the desktop application, although the ribbon layout and keyboard shortcuts can differ from Windows. Excel for the web does not run VBA macros; open the workbook in desktop Excel to use them.
Frequently Asked Questions
What is the maximum IndentLevel in Excel VBA?
Excel supports levels from 0 to 15. Level 0 means no indent; 15 is the maximum. Keep any calculation that produces a level within that range.
How do I remove an indent in VBA?
Set the target cell or range’s IndentLevel property to 0. This clears the formatting indent without deleting spaces or changing the cell’s value.
Can I indent a whole range at once?
Yes. Assign the desired level to a range such as Range("B2:B20"). Every cell in that range receives the assigned level, so use a loop if different rows need different levels.
Does IndentLevel add spaces to the cell’s text?
No. It changes cell formatting only. The displayed content and the value used by formulas remain unchanged; typed leading spaces are a separate part of the text.
Why can’t I run my VBA indent macro in Excel for the web?
Excel for the web can open and edit many macro-enabled workbooks, but it cannot run VBA. Use Excel’s desktop application for Windows or Mac to execute the macro.
Related
- Hanging Indent in Word: A Step-by-Step Guide
- Hanging Indent in Word: A Simple Step-by-Step Guide
- How to Indent Down in Excel: Cells, Text, and Rows
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Hide a Column by Number in Your WorksheetTo hide a column by its number in Excel VBA, set that worksheet column’s Hidden property to True.…
- VBA Range Index: How Cells, Item, and Offsets WorkIn VBA, a range index identifies a cell by its row and column position, starting at 1. For…
- VBA Select All Cells: How to Select the Used RangeThe quickest way to select all cells in an Excel worksheet with VBA is Cells.Select, which selects every…
- Excel VBA Dynamic Named Range: Create and Update ItTo create a dynamic named range with VBA, define a workbook name whose formula adjusts to the current…
- VBA Syntax: Key Rules and ExamplesThe syntax for VBA (Visual Basic for Applications) is the set of rules that defines how to write…
- Macro Functions in SAS: A Practical GuideMacro functions in SAS are pre-written pieces of code that perform text manipulation, evaluation, and data-driven tasks within…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.