Excel VBA to Replace Blank Cells with Text (3 Examples)

We have a dataset of some students containing their names, class, and percentage in the examination. There are some blank cells in the Percentage column. We will replace those blank cells with text.


Example 1 – A Simple VBA to Replace Blank Cells with Text

This VBA will check the value of each cell one by one. When it finds any blank, it will fill that cell with an input text.

Steps:

  • Right-click on the sheet name.
  • Choose the View Code option from the Context Menu.

VBA to Replace Blank Cells with Text

  • Enter the VBA window.
  • Choose the Module option from the Insert tab.

VBA to Replace Blank Cells with Text

  • The VBA command module appears. We will write the following VBA code here.

  • Select all cells of the Percentage column.
  • Put the following VBA code on the command module.
Sub Replace_Blank_With_Text_2()
Dim Range1 As Range
Dim Value_1 As String
On Error Resume Next
Value_1 = InputBox("Replace with", "Replace Blank Cell")
For Each Range1 In Selection
If Range1.Text = "" Then Range1.Value = Value_1
    Next Range1
End Sub

VBA to Replace Blank Cells with Text

  • Run the code by pressing the F5 button.
  • Write the desired text in the input box.

VBA to Replace Blank Cells with Text

  • Press the OK button.

Look all blank cells are filled with Absent.

Code Explanation:

Dim Range1 As Range
Dim Value_1 As String

These two lines declare the variables first.

On Error Resume Next

If any error is found, resume the next section.

Value_1 = InputBox("Replace with", "Replace Blank Cell")

Use InsertBox to input the value of Value_1.

For Each Range1 In Selection

Apply a for statement.

If Range1.Text = "" Then Range1.Value = Value_1
    Next Range1

If the value of the Range1 is blank, then fill it with the value of Value_1.\

Read More: Find and Replace a Text in a Range with Excel VBA


Example 2 – Use the VBA IsEmpty Function

Steps:

  • Choose the cells of the Percentage column.
  • Copy and paste the following VBA code into the VBA command module.
Sub Replace_Blank_With_Text()
Dim Range1 As Range
Dim Value_1 As String
On Error Resume Next
Value_1 = InputBox("Replace with", "Replace Blank Cell")
For Each Range1 In Selection
If IsEmpty(Range1) Then
Range1.Value = Value_1
End If
Next
End Sub

Use VBA IsEmpty Function to replace blank cells

  • Press the F5 button to run the code.
  • A window will appear to put the replacement text. Put your text here.

  • Press OK.

Use VBA IsEmpty Function

All blank cells are filled with text.

Code Explanation:

Dim Range1 As Range
Dim Value_1 As String

These two macros declare the variables first.

On Error Resume Next

If any error is found, resume the next section.

Value_1 = InputBox("Replace with", "Replace Blank Cell")

Use the InputBox method to insert a value.

For Each Range1 In Selection

Apply a for statement.

If IsEmpty(Range1) Then
Range1.Value = Value_1

Check if the value of Range1 is empty or not. If empty then fill the value of Range1 by the Value_1.


Example 3 – Use vbNullString to Replace Blank Cells

Steps:

  • Go to the VBA command module and paste the following VBA code.
Sub Replace_Blank_With_Text_3()
Dim Range1 As Range
For Each Range1 In ActiveSheet.Range("D4:D13")
If Range1 = vbNullString Then Range1 = "Absent"
Next
End Sub

Use vbNullString to Replace Blank Cells

  • Press the F5 button to run the VBA code.

Empty cells are filled with text.

Code Explanation:

Dim Range1 As Range

Declare a variable.

For Each Range1 In ActiveSheet.Range("D4:D13")

Apply a for the statement on Range1, whose range is D4:D13.

If Range1 = vbNullString Then Range1 = "Absent"

If the value of Range1 is Null then fill up that by Absent.


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Alok Paul
Alok Paul

Alok Paul has completed his B.Sc. in Electronics and Telecommunication Engineering from East West University. He has been working on the ExcelDemy project for more than 2 years. He has written 220+ articles and replied to numerous comments. He is experienced in Microsoft Office, especially in Excel. He also led some teams on Excel and VBA content development. He has a keen interest in Advanced Excel, Data analysis, Excel Pivot Table, Charts, and Dashboard. He loves to research... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo