How can I use VBA to set the font size in a document?

VBA (Visual Basic for Applications) is a programming language that can be used to automate tasks in Microsoft Office applications, including setting the font size in a document. By using VBA, users can create a code that can be executed to change the font size of selected text or the entire document. This allows for a quicker and more efficient way of adjusting font sizes, especially in larger documents. VBA can also be used to set specific font sizes for different sections of a document, providing more customization options. Overall, using VBA to set font sizes in a document can help save time and improve the overall formatting of the document.

Set Font Size Using VBA (With Example)


You can use the Font.Size property in VBA to get and set the font size of specific cells in Excel.

For example, you could use the following macro to create a message box that displays the current font size of cell A1:

Sub GetFontSize()
MsgBox Range("A1").Font.Size
End Sub

Or you could create the following macro to set the font size of cell A1 to be 20:

Sub SetFontSize()
Range("A1").Font.Size = 20End Sub

Or you could create the following macro to make the font size in each cell in the range A1:C1 to be 20:

Sub SetFontSize()
Range("A1:C1").Font.Size = 20
End Sub

The following examples show how to get and set font size using VBA with the following dataset in Excel:

Example 1: Get Font Size Using VBA

We can create the following macro to create a message box that displays the current font size of cell A1:

Sub GetFontSize()
MsgBox Range("A1").Font.Size
End Sub

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

The message box tells us that cell A1 currently has a font size of 11.

This is the default font size in Excel.

Example 2: Set Font Size of Specific Cell Using VBA

Sub SetFontSize()
Range("A1").Font.Size = 20
End Sub

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

We can see that the font size of cell A1 has been changed to 20 while the font size of all other cells has remained unchanged.

Example 3: Set Font Size of Range of Cells Using VBA

We can create the following macro to set the font size of each cell in the range A1:C1 to be 20:

Sub SetFontSize()
Range("A1:C1").Font.Size = 20
End Sub

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

We can see that the font size of each cell in the range A1:C1 has been changed to 20 while the font size of all other cells has remained unchanged.

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

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

x