How can I wrap text using VBA?


You can use the WrapText property in VBA to wrap text in specific cells of an Excel worksheet.

Here are three common ways to use this property in practice:

Method 1: Wrap Text of One Specific Cell

Sub UseWrapText()
Range("B2").WrapText = True
End Sub

Method 2: Wrap Text of Cells in Specific Range

Sub UseWrapText()
Range("B2:B11").WrapText = True
End Sub

Method 3: Wrap Text of All Cells in Worksheet

Sub UseWrapText()
Cells.WrapText = True
End Sub

The following examples show how to use each method in practice with the following dataset in Excel:

Let’s jump in!

Example 1: Wrap Text of One Specific Cell Using VBA

We can create the following macro to wrap the text in cell B2 only:

Sub UseWrapText()
Range("B2").WrapText = True
End Sub

When we run this macro, we can click on cell B2 and see that the Wrap Text feature is turned on within the Alignment group of the Home tab along the top ribbon:

However, we must shorten the length of column B and expand the height of row 2 to actually see the text wrapped:

Now we can easily see that the text in cell B2 is wrapped while the text in all other cells of column B are not wrapped.

Example 2: Wrap Text of Cells in Specific Range Using VBA

We can create the following macro to wrap the text in each cell in the range B2:B11:

Sub UseWrapText()
Range("B2:B11").WrapText = True
End Sub

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

We can see that each cell in the range B2:B11 now has wrapped text.

Example 3: Wrap Text of All Cells in Worksheet Using VBA

We can create the following macro to wrap the text of every cell in a worksheet:

Sub UseWrapText()
Cells.WrapText = True
End Sub

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

We can see that each cell in the worksheet now has wrapped text.

Note: You can find the complete documentation for the VBA WrapText property .

x