When working with Microsoft Excel for data analysis, we need to rotate texts in different orientations for proper presentations. We can rotate texts in Excel with several options and techniques. In this article, we will discuss how to rotate text to 90 degrees using Excel VBA codes.
Excel VBA to Rotate Text to 90 Degrees: 4 Examples
We will show 4 VBA code examples to rotate text to 90 degrees. We will use the following dataset for the examples.
1. A Simple Excel VBA Code to Rotate Text to 90 Degrees
Here we will discuss how to rotate text to 90 degrees directly from mentioning the cell or range within the VBA code.
Step 1:
- First, go to the Sheet Name section which is found on the lowest border of a worksheet. Then, press the right button of the mouse.
- Choose the View Code option from the list.
Step 2:
- VBA window will appear.
- Choose the Module option from the Insert tab.
- Now, the VBA command module will appear. We will write VBA code on the module.
Step 3:
- Copy and paste the following VBA code on the command module.
Sub rotate_text_1()
Dim text_1 As Range
Set text_1 = Range("B5")
With text_1
.Orientation = 90
End With
End Sub
Step 4:
- Now, run the VBA code by pressing F5.
Voila! The text of Cell B5 is rotated here.
Now you can adjust the length of the cell if you think of doing so.
2. VBA Code to Turn Text to 90 Degrees from Selection
In this example, we will select the range of cells first. Then, run the given VBA code to rotate the text to 90 degrees.
Step 1:
- Press Alt+F11 to enter the VBA command module.
- Put the following VBA code on the command module.
Sub rotate_text_2()
Dim text_1 As Range
Set text_1 = Application.Selection
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 90
.ShrinkToFit = True
.MergeCells = False
End With
End Sub
- Now, select the range of cells you want to rotate.
Step 2:
- Now, the text is rotated. But cell widths are not properly adjusted. Choose Rows 5 and 6.
- Move the cursor between the cells and double-click the mouse button.
Now, look at the dataset.
Texts are rotated with proper visibility.
3. VBA Code to Rotate Text to 90 Degrees from User’s Input
We will select cells as input after running the VBA code to rotate the texts 90 degrees.
Step 1:
- Hit Alt+F11 to enter the VBA command module.
- Copy the following VBA code and paste it onto the module.
Sub rotate_text_3()
Dim text_1 As Range
Set text_1 = Application.InputBox("Range", "Select a range", Type:=8)
With text_1
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Step 2:
- Press F5 to run the code.
- A dialog box will appear to choose a range. We select Range B5:B6 here.
Step 3:
- We want to fit texts on the cells. Now, choose Rows 5 and 6.
- Move the cursor on the selection. Then, double-click the mouse.
Look at the dataset. Texts of cells are rotated with proper presentation.
Read More: Excel VBA to Set Vertical Alignment
4. Excel VBA to Autofit Text and Rotate to 90 Degrees
In the previous examples, we needed to fit the cells manually. But in this example, we will rotate the texts with an autofit facility.
Step 1:
- Hit Alt+F11 to enter the command module.
- Put the VBA code below on the command module.
Sub rotate_text_4()
Dim text_1 As Range
Set text_1 = Range("B5:B6")
With text_1
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
text_1.EntireRow.AutoFit
text_1.EntireColumn.AutoFit
End Sub
Step 2:
- Press F5 to run the VBA code.
You can see that texts are rotated and autofitted at the same time.
Download Practice Workbook
Conclusion
In this article, we showed examples of Excel VBA to rotate text 90 degrees. I hope this will satisfy your needs. Please give your suggestions in the comment box.