How to Indent in Excel ? (VBA)

In this article, we will learn to apply indentation on cells in Excel. We will learn the normal methods to apply indentation and learn shortcut methods. We will also automate text indentation with the help of VBA code.

So let us start learning.

Indent From Excel Ribbon

Indent in Excel refers to the distance between cell content and cell boundary from the left. Indentation makes the text easier to read and makes the spreadsheet look professional. We can indent the cells from the Excel ribbon using the following steps.

  • Select the range of cells that you wish to indent.
indent in excel
  • Go to the Home tab on the ribbon. You will find two buttons to increase or decrease the indent in the Alignment Group.
increase and decrease indent using VBA
  • You can click on the buttons to increase or decrease the indentation of the selected range of cells.
increase or decrease indentation in excel

You can also use the following shortcuts to increase or decrease indent from the Excel ribbon.

  • Increase Indentation – Alt H 6
  • Decrease Indentation – Alt H 5
infographics indentation in excel vba

Shortcut Method to Indent Text in Excel

We can use the shortcut method to directly specify the magnitude of indentation of the cells from the Format Cells Dialog Box. The indentation value can lie between 1 and 250.

  • Select the range to which you wish to indent.
  • Press Ctrl and 1 key to open the Format Cells dialog box.
  • In the Alignment Group, set the indentation magnitude and click Ok.
indent using shortcut method in excel

Text Indentation Using VBA

We can use the Sub procedure to apply text indentation to the selected cells of the worksheet. We will input the value of indentation using the InputBox function in VBA. The selected range can be referred to using the Selection property.

You can use the following Sub Procedure to do indentation using VBA.

Sub indentSelection()
'indent range of selected cells using VBA
Dim x As Integer
x=InputBox("Enter indentation value: ( from 0 to 255 ):")
Selection.IndentLevel = x
End Sub

Firstly you select the range to apply indentation. Thereafter we run the macro and specify the indentation value. It will be applied to the selected Range.

indentation using VBA in excel

Removing Indent Using VBA

You can use the indentSelection sub procedure and specify the indentation magnitude to 0 and this will remove the indent from the selected range.

remove indent from excel using VBA

This brings us to an end.

Thank you for reading. ❤

Leave a Comment