Table of Contents
This process involves using Visual Basic for Applications (VBA) to delete rows in a spreadsheet based on a specific cell value. VBA is a programming language used in Microsoft Office applications, including Excel. By using VBA, users can automate tasks and manipulate data in a more efficient manner. To delete rows based on a specific cell value, the VBA code will first need to identify the cell or range of cells that contain the value. Then, it will loop through the rows and delete any row that contains the specified value. This method can be useful for quickly removing unwanted data or duplicate entries from a large dataset. It can also be customized to fit the specific needs of the user, making it a versatile and powerful tool for data management in Excel.
VBA: Delete Rows Based on Cell Value
You can use the following syntax in VBA to delete rows based on a cell value:
Sub DeleteRowsByValue()
Dim ws As Worksheet
Set ws = ActiveSheet
'clear existing filtersOn Error Resume Next
ws.ShowAllData
On Error GoTo 0'filter range where column 2 in range is equal to "East"
ws.Range("A1:C10").AutoFilter Field:=2, Criteria1:="East"
'delete rows that are visible
Application.DisplayAlerts = False
ws.Range("A2:C10").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
'remove filterOn Error Resume Next
ws.ShowAllData
On Error GoTo 0End Sub
This particular macro deletes all rows in the range A1:C10 where the value in column B is equal to “East.”
This macro uses the following steps:
- Apply a filter to A1:C10 to only show rows where the value in column B is “East.”
- Then delete all visible cells.
- Then remove the filter.
This has the effect of deleting all rows in the range A1:C10 where the value in column B is equal to “East.”
The following example shows how to use this syntax in practice.
Example: Use VBA to Delete Rows Based on Cell Value
Suppose we have the following dataset that contains information about various basketball players:

Suppose that we would like to delete each row in the dataset where the Conference column is equal to “East.”
We can create the following macro to do so:
Sub DeleteRowsByValue()
Dim ws As Worksheet
Set ws = ActiveSheet
'clear existing filtersOn Error Resume Next
ws.ShowAllData
On Error GoTo 0'filter range where column 2 in range is equal to "East"
ws.Range("A1:C10").AutoFilter Field:=2, Criteria1:="East"
'delete rows that are visible
Application.DisplayAlerts = False
ws.Range("A2:C10").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
'remove filterOn Error Resume Next
ws.ShowAllData
On Error GoTo 0End SubWhen we run this macro, we receive the following output:

Notice that all rows where the value in the Conference column was “East” have been deleted.
Note: The line Application.DisplayAlerts=False tells VBA not to display the process of deleting the visible rows, which speeds up the process.
Cite this article
stats writer (2024). How can I delete rows in VBA based on a specific cell value?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-delete-rows-in-vba-based-on-a-specific-cell-value/
stats writer. "How can I delete rows in VBA based on a specific cell value?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-delete-rows-in-vba-based-on-a-specific-cell-value/.
stats writer. "How can I delete rows in VBA based on a specific cell value?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-delete-rows-in-vba-based-on-a-specific-cell-value/.
stats writer (2024) 'How can I delete rows in VBA based on a specific cell value?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-delete-rows-in-vba-based-on-a-specific-cell-value/.
[1] stats writer, "How can I delete rows in VBA based on a specific cell value?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I delete rows in VBA based on a specific cell value?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
