赛派号

15米长的电视多少寸 How to Format Cell and Center Text with Excel VBA (5 Ways)

Method 1 – Run a VBA Code to Center Text Horizontally and Format Cell with Excel VBA 1.1 Center an Active Cell Horizontally

In the image below; there is an active cell B4 that is horizontally center-aligned. To make it center-aligned using the Excel VBA, follow the steps below.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 1: Create a Module

Press Alt + F11 to open the VBA Macro. Click on Insert. Select Module.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 2: Write a VBA Code

Paste the following VBA code. Sub CenterTextHorizontally() ActiveCell.HorizontalAlignment = xlCenter End Sub

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 3: Run the Program

Se the program and press F5 to run. The cell B4 is center-aligned.

Suitable Ways to Center Text and Format Cell with Excel VBA

1.2 Center Selected Text Horizontally

Step 1: Selection of Cells

Select the desired range.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 2: Type a VBA Code

Enter the following VBA code in a new Module. Sub CenterTextHorizontally() Selection.HorizontalAlignment = xlCenter End Sub

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 3: Get the Result

Se the program and press F5 to run. The selected range will be center-aligned.

Suitable Ways to Center Text and Format Cell with Excel VBA

Read More: Excel VBA Horizontal Alignment

Method 2 – Perform a VBA Code to Center Text Vertically and Format Cell with Excel VBA 2.1 Center an Active Cell Vertically with Excel VBA

Step 1: Enter a VBA Code

Insert a new Module as before. In a new Module, enter the following VBA, Sub VBACenterCellVertical() ActiveCell.VerticalAlignment = xlCenter End Sub Suitable Ways to Center Text and Format Cell with Excel VBA

Step 2: Se and Run the Program

Se the program and press F5 to run the program. The active cell B4 will appear vertically center-aligned.

Suitable Ways to Center Text and Format Cell with Excel VBA

2.2 Center Selected Text Vertically with Excel VBA

Step 1: Selection of Cells

Select all the required cells.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 2: Insert a VBA Code

Create a Module and enter the following VBA. Sub VBACenterCellVertical() Selection.VerticalAlignment = xlCenter End Sub

Suitable Ways to Center Text and Format Cell with Excel VBA

The selected cells will be aligned vertically in the center.

Suitable Ways to Center Text and Format Cell with Excel VBA

Read More: Excel VBA to Set Vertical Alignment

Method 3 – Apply a VBA Code to Center Text Both Horizontally and Vertically with Excel VBA

Step 1: Selection of the cells

Select the required  cells in the range.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 2: Write a VBA Code

Enter the code in a new Module. Sub CenterTextHorizontallyAndVertically() Selection.HorizontalAlignment = xlCenter Selection.VerticalAlignment = xlCenter End Sub

Suitable Ways to Center Text and Format Cell with Excel VBA

The cells in the selection are aligned centrally.

Suitable Ways to Center Text and Format Cell with Excel VBA

Method 4 – Run a VBA Code to Center Text and Format Cell Orientation with Excel VBA

Step 1: Selection of the cells

Select the cells.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 2: Enter a VBA code

In a new Module, enter the following VBA, Sub FormatOrientation() With Selection 'Apply command to align horizontally .HorizontalAlignment = xlCenter 'Write command to align Vertically .VerticalAlignment = xlCenter 'Orientation by clockwise 30 degree .Orientation = -30 'Merge the selected cells .MergeCells = True End With End Sub

Suitable Ways to Center Text and Format Cell with Excel VBA

Cell B4 is rotated 40 degrees clockwise.

Suitable Ways to Center Text and Format Cell with Excel VBA

Step 3: Repeat the Selection

Repeat the process for the remaining blank cells.

Sample Data

Step 4: Enter a VBA code

To apply the code to a selected cell, enter the VBA code in a new Module. Sub FormatOrientation() With Selection 'Apply command to align horizontally .HorizontalAlignment = xlCenter 'Write command to align Vertically .VerticalAlignment = xlCenter 'Orientation by clockwise 30 degree .Orientation = -30 'Merge the selected cells .MergeCells = True End With End Sub

Suitable Ways to Center Text and Format Cell with Excel VBA

Run the program to get the result.

Sample Data

Read More: Excel VBA to Rotate Text to 90 Degrees

Method 5 – Perform a VBA Code to Center Text and Format Cell Font Color with Excel VBA

Step 1: Selection of the Cells

Select the cells needed to merge and add color.

Sample Data

Step 2: Paste the VBA Codes

Enter the following VBA code. Sub FormatFont() With Selection 'Apply command to align horizontally .HorizontalAlignment = xlCenter 'Write command to align Vertically .VerticalAlignment = xlCenter 'Enter RGB value for a font color .Font.Color = RGB(128, 0, 0) 'Orientation by clockwise -45 degree .Orientation = -45 'Merge the selected cells .MergeCells = True End With End Sub

Sample Data

Step 3: Run the Program

Se the program and press F5 to run it. The cells will be merged and colored with the RGB color code (128,0,0).

Sample Data

Apply the code to the other selections.

Sample Data

 

Sample Data

Download Practice Workbook

Center Text with VBA.xlsm Get FREE Advanced Excel Exercises with Solutions! SeSedRemoved 0

Tags: VBA Alignment

Bhubon Costa Bhubon Costa

Bhubon Costa, B.Sc. in Nal Architecture & Marine Engineering from Bangladesh University of Engineering & Technology, has worked with the ExcelDemy since 2021. Currently, he has been working as a reviewer. Notably, he has written over 90 articles and led several VBA content development teams. He has a great passion for the fields of data analytics and data science. His areas of expertise include Excel VBA, Power Query, Pivot Table, Power BI, MySQL, PostgreSQL, machine learning, and Python... Read Full Bio

We will be happy to hear your thoughts Lee a reply Cancel reply

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了