Excel VBA Code to Delete Rows Based on Multiple Cell Value (3 Criteria)

In this article, I’ll show you the VBA code to delete rows in Excel based on multiple cell value. You’ll learn to delete rows when multiple cells depend on a single value, as well as when single cells depend on multiple values. First I’ll show you the VBA code, then I’ll show you how to run the code step by step.


Download Practice Workbook


3 Easy VBA Codes to Delete Rows in Excel Based on Multiple Cell Value

Here we’ve got a data set with the Names of some students, along with their Marks in Physics, Chemistry, and Mathematics, of a school called Sunflower Kindergarten.

Data Set to Use VBA codes to delete rows based on multiple cell value in Excel

Today our objective is to use VBA code to delete rows in Excel based on multiple cells values from this data set.


1. VBA Code to Delete Rows when a Single Cell Depends on a Single Value

You can use VBA code to delete rows in Excel when a single cell depends on a value from this data set.

For example, let’s try to delete the rows with the students who got 100 in Physics.

You can use the following VBA code to accomplish this.

VBA Code: 

Sub Delete_Rows_When_Single_Cell_Depends_on_Single_Value()


Condition = Int(InputBox("Enter 1 to Delete Rows with a Value Greater than Some Value: " + vbNewLine + "Enter 2 to Delete Rows with a Value Greater than or Equal to Some Value: " + vbNewLine + "Enter 3 to Delete Rows with a Value Less than Some Value: " + vbNewLine + "Enter 4 to Delete Rows with a Value Less than or Equal to Some Value: " + vbNewLine + "Enter 5 to Delete Rows with a Value Equal to Some Value: " + vbNewLine + "Enter 6 to Delete Rows with a Value Not Equal to Some Value: " + vbNewLine + "Enter 7 to Delete Rows with Partial Match: "))

Value = InputBox("Enter the Value: ")

Column_Number = Int(InputBox("Enter the Number of the Column where the Criteria will be Applied: "))

If Condition = 1 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        If Selection.Cells(i, Column_Number) > Value Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
        End If
    Next i

ElseIf Condition = 2 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        If Selection.Cells(i, Column_Number) >= Value Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
        End If
    Next i

ElseIf Condition = 3 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        If Selection.Cells(i, Column_Number) < Value Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
        End If
    Next i

ElseIf Condition = 4 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        If Selection.Cells(i, Column_Number) <= Value Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
        End If
    Next i

ElseIf Condition = 5 Then
    For i = 1 To Selection.Rows.Count
        If VarType(Selection.Cells(i, Column_Number)) <> 8 Then
            On Error Resume Next
            Value = Int(Value)
        End If
        If Selection.Cells(i, Column_Number) = Value Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
        End If
    Next i

ElseIf Condition = 6 Then
    For i = 1 To Selection.Rows.Count
        If VarType(Selection.Cells(i, Column_Number)) <> 8 Then
            On Error Resume Next
            Value = Int(Value)
        End If
        If Selection.Cells(i, Column_Number) <> Value And Selection.Cells(i, Column_Number) <> "" Then
            Selection.Cells(i, 1).EntireRow.Delete
            i = i - 1
        End If
    Next i

ElseIf Condition = 7 Then
    For i = 1 To Selection.Rows.Count
        If VarType(Selection.Cells(i, Column_Number)) <> 8 Then
            On Error Resume Next
            Cell_Value = Str(Selection.Cells(i, Column_Number))
        Else
            Cell_Value = Selection.Cells(i, Column_Number)
        End If
        For j = 1 To Len(Cell_Value)
            If Mid(Cell_Value, j, Len(Value)) = Value Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
                Exit For
            End If
        Next j
    Next I

End If


End Sub

Note: This code creates a Macro called Delete_Rows_When_Single_Cell_Depends_on_Single_Value.

Step by Step Procedure to Run This Code:

Step 1: Opening the VBA Window

Press ALT+F11 on your keyboard. The VBA window will open.

VBA Window to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Step 2: Inserting a New Module

Go to the Insert tab in the VBA window. 

From the options available, select Module.

Step 3: Entering the VBA Code

A new module window called “Module 1” will open. 

Insert the given VBA code in the opened Module.

VBA Code to Delete Rows based on Multiple Cell Value in Excel

Step 4: Saving the Macro-Enabled Workbook

Save the workbook as Excel Macro-Enabled Workbook.

Saving Workbook to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Step 5: Selecting the Data Set

➤ Return to your worksheet.

Select the range of cells from where you want to delete the rows (Without Column Headers).

As I want to delete the rows where the marks in Physics are 100, I have selected the column Marks in Physics (The rows to be deleted are highlighted in light brown).

Step 6: Running the Macro

➤ Then press ALT+F8 on your keyboard.

➤ A dialogue box called Macro will open. Select Delete_Rows_When_Single_Cell_Depends_on_Single_Value (The name of the Macro) and click on Run.

Running Macro to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Step 7: Entering the Inputs 

  You will get three Input Boxes. The 1st box will ask you to enter a number between 1 to 6 depending on your condition.

As my condition is to be equal to a value (100), I have entered 5.

Input Box to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

The 2nd box will ask you to enter the value to which you want to compare. In this example, it is 100.

And the 3rd Input Box will ask for the number of the column where the condition will be applied.

In this example, it’s 2 (Marks in Physics).

Input Box to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Step 8: The Final Output!

Then click OK.

you will find the rows that satisfy your condition (Marks of Physics equal to 100 in this example) deleted automatically.

Output to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Things to Remember:

  • You can also use this code to delete rows with greater than, less than, or not equal to a value. Select your required condition from the 1st Input Box.
  • For example, you can delete the rows with Marks in Chemistry less than 40 or Marks in Mathematics greater than 80. Try yourself.
  • When the condition is equal to some value, you can match both String and Integer type values.
  • For example, here we’ve deleted the rows with marks equal to 100. You can also delete the rows where the name is Ross Hayes or anything. Try yourself.
  • You can use this code to delete rows with partial matches too. Enter 7 in the 1st Input Box.
  • For example, you can delete all the rows where the surname is Hopkins. Try yourself.
  • And in the case of partial matches, it will also work for both String and Integer type values.
  • For example, like you can delete the rows with the surname Hopkins, you can also delete the rows that contain a 5 within the mark in Physics (45, 50, 523, 53, 65, etc.). Try yourself.

Read More: How to Delete Row Using VBA (14 Ways)


2. VBA Code to Delete Rows when Multiple Cells Depend on a Single Value

Now we’ll develop a Macro to delete rows when multiple cells depend on a single value.

For example, let’s try to delete the rows where the marks in Physics and Mathematics are greater than 80.

You can use the following VBA code to accomplish this:

VBA Code: 

Sub Delete_Rows_When_Multiple_Cells_Depend_on_Single_Value()

Condition = Int(InputBox("Enter 1 to Delete Rows with a Value Greater than Some Value: " + vbNewLine + "Enter 2 to Delete Rows with a Value Greater than or Equal to Some Value: " + vbNewLine + "Enter 3 to Delete Rows with a Value Less than Some Value: " + vbNewLine + "Enter 4 to Delete Rows with a Value Less than or Equal to Some Value: " + vbNewLine + "Enter 5 to Delete Rows with a Value Equal to Some Value: " + vbNewLine + "Enter 6 to Delete Rows with a Value Not Equal to Some Value: " + vbNewLine + "Enter 7 to Delete Rows with Partial Match: "))

Value = InputBox("Enter the Value: ")

Column_Numbers = InputBox("Enter the Number of the Columns where the Criteria will be Applied: ")

Column_Numbers = Split(Column_Numbers, ",")

Condition_Type = Int(InputBox("Enter 0 for OR Type Criteria: " + vbNewLine + "OR" + vbNewLine + "Enter 1 for AND Type Criteria: "))

Dim Count As Integer

If Condition = 1 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If Selection.Cells(i, Int(Column_Numbers(j))) > Value Then
                Count = Count + 1
            End If
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i

ElseIf Condition = 2 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If Selection.Cells(i, Int(Column_Numbers(j))) >= Value Then
                Count = Count + 1
            End If
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i

ElseIf Condition = 3 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If Selection.Cells(i, Int(Column_Numbers(j))) < Value Then
                Count = Count + 1
            End If
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i
    
ElseIf Condition = 4 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If Selection.Cells(i, Int(Column_Numbers(j))) <= Value Then
                Count = Count + 1
            End If
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i

ElseIf Condition = 5 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        If VarType(Selection.Cells(i, 1)) <> 8 Then
            On Error Resume Next
            Value = Int(Value)
        End If
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If Selection.Cells(i, Int(Column_Numbers(j))) = Value Then
                Count = Count + 1
            End If
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i

ElseIf Condition = 6 Then
    Value = Int(Value)
    For i = 1 To Selection.Rows.Count
        If VarType(Selection.Cells(i, 1)) <> 8 Then
            On Error Resume Next
            Value = Int(Value)
        End If
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If Selection.Cells(i, Int(Column_Numbers(j))) <> Value And Selection.Cells(i, Int(Column_Numbers(j))) <> "" Then
                Count = Count + 1
            End If
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i


ElseIf Condition = 7 Then
    For i = 1 To Selection.Rows.Count
        Count = 0
        For j = 0 To UBound(Column_Numbers)
            If VarType(Selection.Cells(i, Int(Column_Numbers(j)))) <> 8 Then
               On Error Resume Next
               Cell_Value = Str(Selection.Cells(i, Int(Column_Numbers(j))))
            Else
                Cell_Value = Selection.Cells(i, Int(Column_Numbers(j)))
            End If
            For k = 1 To Len(Cell_Value)
                If Mid(Cell_Value, k, Len(Value)) = Value Then
                    Count = Count + 1
                    Exit For
                End If
            Next k
        Next j
        If Condition_Type = 0 Then
            If Count > 0 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        Else
            If Count = UBound(Column_Numbers) + 1 Then
                Selection.Cells(i, 1).EntireRow.Delete
                i = i - 1
            End If
        End If
    Next i
End If

End Sub

Note: This code creates a Macro called Delete_Rows_When_Multiple_CellsDepend_on_Single_Value.

Step by Step Procedure to Run the Code:

Execute steps 1-4 from method 1 to open the VBA window, insert a new module, enter the VBA code and save the workbook.

Then select the data set from your worksheet.

As I’ll delete the rows where the marks in Physics and Mathematics are greater than 80, they are highlighted light brown to visualize.

Selecting Data Set to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Then press ALT+F8, select Delete_Rows_When_Multiple_CellsDepend_on_Single_Value, and click on Run.

Running Macro to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

You will get four Input Boxes. The 1st one will ask for a number between 1 to 6 according to the required condition.

In this example, it is 1 (Greater than 80).

The 2nd box will ask for the value. It’s 80.

Entering Input to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

The 3rd box will ask for the numbers of the columns where the condition will be applied.

In this example, it’s 2,4 (Marks in Physics and Marks in Mathematics).

Separate them by a comma (Don’t put any space in between).

Entering Input to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

And the final box will ask for the OR Type or the AND Type criteria.

Enter 0 for OR Type criteria.

And enter 1 for AND Type criteria.

Here it’s 1. We want the marks to be greater than 80 in both subjects.

Finally, click OK. You’ll find all the rows that fulfill the criteria deleted automatically.

Output to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Things to Remember:

  • You can use this code for all types of conditions like greater than, less than, equal to, not equal to, partial match, etc. Select your required one from the 1st Input Box (Same as Code 1).
  • Equal to, not equal to and the partial match will work for String and Integer type values (Same as the 1st code).
  • You can apply the condition on any number of columns within your data set. Enter them in Input Box 3.
  • You can apply both AND Type and OR Type criteria to the data set. Select accordingly on Input Box 4.

Read More: How to Use Macro to Delete Rows Based on Criteria in Excel (3 Ways)


Similar Readings:


3. VBA Code to Delete Rows when Single Cell Depends on Multiple Values

Now, we’ll develop a VBA code to delete rows when a single cell depends on multiple values.

For example, let’s try to delete the rows if the Marks in Physics are equal to 100, 82, or 85.

The VBA code will be:

VBA Code: 

Sub Delete_Rows_When_Single_Cell_Depends_on_Multiple_Values()

Condition = Int(InputBox("Enter 1 for an Exact Match: " + vbNewLine + "OR" + vbNewLine + "Enter 2 for a Partial Match:"))

Values = InputBox("Enter the Values: ")

Values = Split(Values, ",")

Column_Number = Int(InputBox("Enter the Number of the Columns where the Criteria will be Applied: "))

If Condition = 1 Then
    For i = 1 To Selection.Rows.Count
        For j = 0 To UBound(Values)
            If VarType(Selection.Cells(i, Column_Number)) <> 8 Then
                On Error Resume Next
                If Selection.Cells(i, Column_Number) = Int(Values(j)) Then
                    Selection.Cells(i, Column_Number).EntireRow.Delete
                    i = i - 1
                    Exit For
                End If
            Else
                If Selection.Cells(i, Column_Number) = Values(j) Then
                    Selection.Cells(i, Column_Number).EntireRow.Delete
                    i = i - 1
                    Exit For
                End If
            End If
        Next j
    Next i

ElseIf Condition = 2 Then
    For i = 1 To Selection.Rows.Count
        For j = 0 To UBound(Values)
            If VarType(Selection.Cells(i, Column_Number)) <> 8 Then
                On Error Resume Next
                If InStr(Selection.Cells(i, Column_Number), Int(Values(j))) = 1 Then
                    Selection.Cells(i, Column_Number).EntireRow.Delete
                    i = i - 1
                    Exit For
                End If
            Else
                If InStr(Selection.Cells(i, Column_Number), Values(j)) Then
                    Selection.Cells(i, Column_Number).EntireRow.Delete
                    i = i - 1
                    Exit For
                End If
            End If
        Next j
    Next i

End If

End Sub

Note: This code creates a Macro called Delete_Rows_When_Single_Cell_Depends_on_Multiple_Values.

Step by Step Procedure to Run the Code:

Execute steps 1-4 from method 1 to open the VBA window, insert a new module, enter the VBA code and save the workbook.

VBA Code to Delete Rows Based on Multiple Cell Value in Excel

Then select the data set from your worksheet.

As I’ll delete the rows where the mark in Physics is 100, 85, or 80, they are highlighted in light brown to visualize.

Then press ALT+F8, select Delete_Rows_When_Single_Cell_Depends_on_Multiple_Values, and click on Run.

Running Macro to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

You will get three Input Boxes. The 1st one will ask for a number between 1 to 2 according to the required condition.

1 for an Exact Match.

2 for a Partial Match.

In this example, it is 1 (Exact Match).

Entering Input to Use VBA Code to Delete Rows Based on Multiple Cell Value in Excel

The 2nd box will ask for the values to match. Enter them separated by commas (,).

Here I have entered 100,85,82 (Don’t put any space in between).

And The 3rd box will ask for the number of the column where the condition will be applied.

In this example, it’s 2 (Marks in Physics).

Entering Input to Use VBA Code to Delete Rows based on Multiple Cell Value in Excel

Then click OK.

You will get the rows that fulfill the desired criteria deleted automatically.

Things to Remember:

  • You can only apply exact match and partial match conditions in this code. Not other conditions like greater than, less than, etc.
  • This will be applicable for both String and Integer data types.

Read More: How to Delete Multiple Rows in Excel with Condition (3 Ways)


Conclusion

Using these methods, you can use VBA code to delete rows in Excel based on multiple cell values. Do you have any questions? Feel free to ask us.


Related Readings

 

Rifat Hassan

Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from the Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo