Excel VBA Codes to Bold Text in a String (5 Examples)

In this tutorial, you will learn how to make a text bold in a string using Excel VBA, along with other related tasks.


Example 1 – Bold the Whole Text String

To bold a text string using VBA, we apply the Font.Bold property.

The Generic Code:

Range.Font.Bold = True

Have a look at the following dataset:

Let’s make the text strings bold using VBA code.

STEPS:

  • Press B on your keyboard to open the Visual Basic Editor window.
  • Click on Insert > Module.
  • Enter the following code in the Module box:
Sub bold_string()

Cells(5, 2).Font.Bold = True

End Sub

Here, we used the Cells method to select Cell B5.

  • Run the code by pressing F5 and you will see the following:

How to Bold a Text with Excel VBA

We can alternatively use the Range method to make the whole string bold.

  • Enter the following code in the Module box:
Sub bold_range_string()

Range("B5").Font.Bold = True

End Sub
  • Press F5 to run the code.

It will give you the same result:

How to Bold a Text with Excel VBA

To make the Range of cells B5:B9 bold, enter the following code:

Sub bold_column()

Range("B5:B9").Font.Bold = True

End Sub

How to Bold a Text with Excel VBA

Read More: How to Format Text in Excel Cell


Example 2 – Bold Partial Text in a String

Now let’s bold specific text within a string using Excel VBA.

Have a look at the screenshot:

Let’s search these strings for the text “Computer” and bold all instances throughout the column.

  • Enter the following code in the Module window:
Sub bold_text_in_string()

Dim r As Range

Dim cell As Range

Set r = Range("B5:B10")

text_value = InputBox("Please Enter Text You Want to Search and Bold")

For Each cell In r

If InStr(cell.Text, text_value) Then

  cell.Characters(WorksheetFunction.Find(text_value, cell.Value), Len(text_value)).Font.Bold = True

End If

Next

End Sub

Set r = Range(“B5:B10”): Selects the range of cells of our dataset.

text_value = InputBox(“Please Enter Text You Want to Search and Bold”): Takes the user input and stores it in text_value.

For Each cell In r: Runs a loop on our dataset.

If InStr(cell.Text, text_value)

cell.Characters(WorksheetFunction.Find(text_value, cell.Value), Len(text_value)).Font.Bold = True: Checks whether we have the specific text in the cell or not. If it finds the matching text in the string, it makes that text bold.

To search the text in the string we used the VBA InStr function.

After running the VBA code, you will see the following output:

VBA to Bold Text in a String in Excel

  • Type the text you want to search and bold in the String. We want to bold the text “Computer”.
  • Click on OK.

VBA to Bold Text in a String in Excel

Our VBA code successfully finds and bolds the text “Computer” in each string in the column.


Example 3 – Bold Selected Cells Containing Text or String

We can bold selected cells containing text and/or strings using the following VBA code:

Sub bold_selected_cell()

Dim r As Range

Set r = Selection

r.Font.Bold = True

End Sub
  • Select the cells you want to bold:

Bold Selected Cells of Text or String in Excel

  • Press Alt+F8 on your keyboard to open a macro dialog box.

  • Select bold_selected_cells.
  • Click on Run.

Bold Selected Cells of Text or String in Excel

As you can see, our Excel VBA code bolds the selected cells containing text or string in the dataset.

Read More: Excel VBA Codes to Format Cell as Text


Example 4 – Bold Cells Containing Specific Text

In addition to bolding text within a string, we can bold an entire string containing that specific text using VBA code.

We revert to the previous dataset:

Let’s make all of the entire strings containing the text “Computer” bold using VBA.

  • Enter the following VBA code in the Module box:
Sub bold_entire_string()

Dim r As Range

Dim cell As Range

Set r = Range("B5:B10")

text_value = InputBox("Please Enter Your Desired Text")

For Each cell In r

If InStr(cell.Text, text_value) Then

  cell.Font.Bold = True

End If

Next

End Sub

Set r = Range(“B5:B10”): Takes the range of cells of our dataset.

text_value = InputBox(“Please Enter Your Desired Text”): Takes the user input and stores it in text_value.

For Each cell In r: Runs a loop through our dataset.

If InStr(cell.Text, text_value) Then

  cell.Font.Bold = True: Checks whether we have the specific text in the cell or not. If it finds the matching text in the string, it makes the entire string bold.

After running the VBA code (press F5), you will see the following output:

Bold Cells Having Specific Text Using VBA

  • Type the text you want to search for. We want to bold the strings having the text “Computer”.
  • Click on OK.

Bold Cells Having Specific Text Using VBA

As you can see, we have successfully bolded the cells of the strings containing specific text.


Example 5 – Bold Cells Based on IF Condition

All the examples above contained IF conditions in them. We basically made a text or string bold based on specific conditions. Here’s another example of how to bold cells based on IF Conditions using Excel VBA.

Have a look at the following dataset:

Here we have a dataset of students’ marks. The passing mark in 32. Our goal is to determine whether the student passed or failed. If someone passes, we will remark them with the text “Passed”, else we will remark them with the text “Failed”. We will also bold the cells containing the text “Failed”.

Enter the following code in the Module box:

Sub bold_condition()

Dim cell, rng As Range

Set rng = Range("C5:C10")

For Each cell In rng

    If cell.Value > 32 Then

        cell.Offset(0, 1).Value = "Passed"

    Else

        cell.Offset(0, 1).Value = "Failed"

        cell.Offset(0, 1).Font.Bold = True

    End If

Next cell

End Sub

Set rng = Range(“C5:C10”): Sets the range of marks.

If cell.Value > 32 Then: Checks whether the cell value is greater than 32 or not.

cell.Offset(0, 1).Value = “Passed”: If the cell value is greater than 32, it will set the adjacent cell value to “Passed”.

cell.Offset(0, 1).Value = “Failed”: If the cell value is less than 32, it will set the adjacent cell value to “Failed”.

To bold the text, we use the following:

cell.Offset(0, 1).Font.Bold = True: After setting the adjacent cell value to “Failed”, it makes the font bold.

Run the code and you will see the following:

Bold Cells Based on IF Condition Using VBA


How to Bold an Entire Row Using VBA

We have learned how to bold text in a string with Excel VBA codes in the previous sections. Now let’s bold an entire row using VBA codes.

There are two methods:

First, you can use the row number to bold the entire row by using the following code:

Sub bold_entire_row()

Rows(7).Font.Bold = True

End Sub

This will bold the entire row 7.

Or you can select a range of row 7 and make the entire row bold using the EntireRow method:

Sub bold_entire_row2()

Range("B7").EntireRow.Font.Bold = True

End Sub

In both cases, running the code will return the following output:

Bold Entire Row Using VBA in Excel

Make the First Row Bold with VBA

Similar to the previous method, you can make just the first row of text or a string bold using the Row number method or the EntireRow method.

Row Number Method:

Sub bold_first_row()

Rows(1).Font.Bold = True

End Sub

This will bold the entire row 1.

EntireRow Method:

Sub bold_entire_row2()

Range("A1").EntireRow.Font.Bold = True

End Sub

This will also bold the entire row 1.


How to Unbold Text or String Using VBA

If you have a text string that is already bold, you can unbold it by setting the Font.Bold property to False.

The Generic Syntax:

Range.Font.Bold= False

Consider the following code:

Sub unbold_text()

Range("B5:B10").EntireRow.Font.Bold = False

End Sub

This code will unbold the specific range of cells:

How to Unbold Text or String Using VBA

Download Practice Workbook


Things to Remember

To make a text or string bold, select that cell and set Font.Bold to True.

✎ To bold a specific range of cells, use the Range method.


Related Articles


<< Go Back to Text Formatting | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
A.N.M. Mohaimen Shanto
A.N.M. Mohaimen Shanto

A.N.M. Mohaimen Shanto, a B.Sc. in Computer Science and Engineering from Daffodil International University, boasts two years of experience as a Project Manager at Exceldemy. He authored 90+ articles and led teams as a Team Leader, meticulously reviewing over a thousand articles. Currently, he focuses on enhancing article quality. His passion lies in Excel VBA, Data Science, and SEO, where he enjoys simplifying complex ideas to facilitate learning and growth. His journey mirrors Exceldemy's dedication to excellence and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo