I would like to insert a row with formatting.

Inserting a row with formatting is the process of adding a new row to a spreadsheet or database table, and adjusting the formatting to match the existing rows. This could include changing the font, size, color, background color, and alignment of the new row. It can also include adjusting the row height and column width to fit the data properly. Inserting a row with formatting can help maintain the overall look and consistency of the table.


You can use the following syntax in VBA to insert a row that has the same formatting as the row above it:

Sub insertRowWithFormatting()

ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats

Application.CutCopyMode = False

End Sub

This particular macro will insert one row below the currently selected cell with the exact same formatting as the cells in the currently selected row.

Note: The line Application.CutCopyMode = False specifies that the cut and copy mode should be turned off after running the macro.

The following example shows how to use this syntax in practice.

Related:

Example: Insert Row with Formatting in VBA

Suppose we have the following dataset in Excel that contains information about various basketball players:

Suppose we would like to insert a row below row 2 with the exact same formatting.

We can create the following macro to do so:

Sub insertRowWithFormatting()

ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats

Application.CutCopyMode = False

End Sub

We can then select cell A2 and then run this macro.

When we run this macro, we receive the following output:

Notice that the new row we inserted has the exact same formatting as the cells in row 2 including the cell colors and the border.

Also notice that all other rows in the existing dataset were simply pushed down.

x