Excel delete row if vba excel

I am trying to come up with a way to delete all rows (and shift cells up, if possible) where the website column cell contains the word none. The table contains 5000+ records and this would save me a great amount of time.

I appreciate any suggestions.

user's user avatar

user

10.9k6 gold badges23 silver badges83 bronze badges

asked Jul 12, 2013 at 1:10

AnchovyLegend's user avatar

AnchovyLegendAnchovyLegend

12k35 gold badges144 silver badges229 bronze badges

1

This is not necessarily a VBA task — This specific task is easiest sollowed with Auto filter.

1.Insert Auto filter (In Excel 2010 click on home-> (Editing) Sort & Filter -> Filter)
2. Filter on the ‘Websites’ column
3. Mark the ‘none’ and delete them
4. Clear filter

answered Jul 12, 2013 at 10:55

MBK's user avatar

1

Ok I know this for VBA but if you need to do this for a once off bulk delete you can use the following Excel functionality: http://blog.contextures.com/archives/2010/06/21/fast-way-to-find-and-delete-excel-rows/ Hope this helps anyone

Example looking for the string «paper»:

  1. In the Find and Replace dialog box, type «paper» in the Find What box.
  2. Click Find All, to see a list of cells with «paper»
  3. Select an item in the list, and press Ctrl+A, to select the entire list, and to select all the «paper» cells on the worksheet.
  4. On the Ribbon’s Home tab, click Delete, and then click Delete Sheet Rows.

Matt's user avatar

Matt

44.3k8 gold badges77 silver badges115 bronze badges

answered Oct 2, 2014 at 11:33

Barry Carter's user avatar

3

In the «Developer Tab» go to «Visual Basic» and create a Module. Copy paste the following. Remember changing the code, depending on what you want. Then run the module.

  Sub sbDelete_Rows_IF_Cell_Contains_String_Text_Value()
    Dim lRow As Long
    Dim iCntr As Long
    lRow = 390
    For iCntr = lRow To 1 Step -1
        If Cells(iCntr, 5).Value = "none" Then
            Rows(iCntr).Delete
        End If
    Next
    End Sub

lRow : Put the number of the rows that the current file has.

The number «5» in the «If» is for the fifth (E) column

answered Jun 1, 2015 at 9:29

Datacrawler's user avatar

DatacrawlerDatacrawler

2,7508 gold badges46 silver badges98 bronze badges

I’d like to add to @MBK’s answer. Although I found @MBK’s answer to be very helpful in solving a similar problem, it’d be better if @MBK included a screenshot of how to filter a particular column.enter image description here

answered Sep 8, 2015 at 16:13

palmbardier's user avatar

This was alluded to in another comment, but you could try something like this.

Sub FilterAndDelete()

Application.DisplayAlerts = False 

     With Sheet1 'Change this to your sheet name

         .AutoFilterMode = False   
         .Range("A3:K3").AutoFilter
         .Range("A3:K3").AutoFilter Field:=5, Criteria1:="none"
         .UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete 

     End With

Application.DisplayAlerts = True

End Sub

I haven’t tested this and it is from memory, so it may require some tweaking but it should get the job done without looping through thousands of rows. You’ll need to remove the 11-Jul so that UsedRange is correct or change the offset to 2 rows instead of 1 in the .Offset(1,0).

Generally, before I do .Delete I will run the macro with .Select instead of the Delete that way I can be sure the correct range will be deleted, that may be worth doing to check to ensure the appropriate range is being deleted.

answered Jul 12, 2013 at 2:44

Soulfire's user avatar

SoulfireSoulfire

4,21821 silver badges32 bronze badges

Try this …

Dim r as Range
Dim x as Integer

For x = 5000 to 4 step -1 '---> or change as you want //Thanx 4 KazJaw

  set r = range("E" & format(x))
  if ucase(r.Value) = "NONE" then
    Rows(x).EntireRow.Delete
  end if 

Next

answered Jul 12, 2013 at 2:14

matzone's user avatar

matzonematzone

5,6933 gold badges17 silver badges20 bronze badges

2

  1. Delete rows 1 and 2 so that your headings are on row 1
  2. Put this in a macro (IT WILL CHECK THROUGH ROW 75000, YOU CAN LOWER THE NUMBER IF YOU WOULD LIKE

    Columns("E:E").Select
    Selection.AutoFilter
    ActiveSheet.Range("$E$1:$E$75000").AutoFilter Field:=1, Criteria1:="none"
    Range("E2:E75000").SpecialCells(xlCellTypeVisible).Select
    Selection.EntireRow.Delete
    ActiveSheet.Cells.EntireRow.Hidden = False
    ActiveSheet.Range("$E$1:$E$75000").AutoFilter Field:=1
    Columns("E:E").Select
    Selection.AutoFilter
    Range("E2").Select
    Range("A1").Select

user3788685's user avatar

user3788685

2,8735 gold badges24 silver badges44 bronze badges

answered Mar 26, 2020 at 17:58

Michael's user avatar

When working with large data sets, you may have the need to quickly delete rows based on the cell values in it (or based on a condition).

For example, consider the following examples:

  1. You have sales rep data and you want to delete all the records for a specific region or product.
  2. You want to delete all the records where the sale value is less than 100.
  3. You want to delete all rows where there is a blank cell.

There are multiple ways to skin this data cat in Excel.

The method you choose to delete the rows will depend on how your data is structured and what’s the cell value or condition based on which you want to delete these rows.

In this tutorial, I will show you multiple ways to delete rows in Excel based on a cell value or a condition.

Filter Rows based on Value/Condition and Then Delete it

One of the fastest ways to delete rows that contain a specific value or fulfill a given condition is to filter these. Once you have the filtered data, you can delete all these rows (while the remaining rows remain intact).

Excel filter is quite versatile and you can filter based on many criteria (such as text, numbers, dates, and colors)

Let’s see two examples where you can filter the rows and delete them.

Delete Rows that contain a specific text

Suppose you have a data set as shown below and you want to delete all the rows where the region is Mid-west (in Column B).

Sales Dataset with US regions

While in this small dataset you can choose to do delete these rows manually, often your datasets are going to be huge where deleting rows manually won’t be an option.

In that case, you can filter all the records where the region is Mid-West and then delete all these rows (while keeping the other rows intact).

Below are the steps to delete rows based on the value (all Mid-West records):

  1. Select any cell in the data set from which you want to delete the rows
  2. Click on the Data tab Click the Data tab in the ribbon
  3. In the ‘Sort & Filter’ group, click on the Filter icon. This will apply filters to all the headers cells in the datasetClick on the Filter icon
  4. Click on the Filter icon in the Region header cell (this is a small downward-pointing triangle icon at the top-right of the cell)Click the Filter icon in the header
  5. Deselect all the other options except the Mid-West option (a quick way to do this is by clicking on the Select All option and then clicking on the Mid-West option). This will filter the dataset and only show you records for Mid-West region.Filter option to get only those rows that you want to delete
  6. Select all the filtered records
  7. Right-click on any of the selected cells and click on ‘Delete Row’Click on Delete Row Option
  8. In the dialog box that opens, click on OK. At this point, you will see no records in the dataset.Click OK to delete rows based on the value
  9. Click the Data tab and click on the Filter icon. This will remove the filter and you will see all the records except the deleted ones.

The above steps first filter the data based on a cell value (or can be other condition such as after/before a date or greater/less than a number). Once you have the records, you simply delete these.

Some useful shortcuts to know to speed up the process:

  1. Control + Shift + L to apply or remove the filter
  2. Control + – (hold the control key and press the minus key) to delete the selected cells/rows

In the above example, I had only four distinct regions and I could manually select and deselect it from the Filter list (in steps 5 above).

In case you have a lot of categories/regions, you can type the name in the field right above the box (that has these region names), and Excel will show you only those records that match entered text (as shown below). Once you have the text based on which you want to filter, hit the Enter key.

Enter text in filter filed to get matching records

Note that when you delete a row, anything that you may have in other cells in these rows will be lost. One way to get around this is to create a copy of the data in another worksheet and delete the rows in the copied data. Once done, copy it back in place of the original data.

Or

You can use the methods shown later in this tutorial (using the Sort method or the Find All Method)

Delete Rows Based on a Numeric Condition

Just as I used the filter method to delete all the rows that contain the text Mid-West, you can also use a number condition (or a date condition).

For example, suppose I have the below dataset and I want to delete all the rows where the sale value is less than 200.

Below are the steps to do this:

  1. Select any cell in the data
  2. Click on the Data tab
  3. In the ‘Sort & Filter’ group, click on the Filter icon. This will apply filters to all the headers cells in the datasetClick on the Filter icon
  4. Click on the Filter icon in the Sales header cell (this is a small downward-pointing triangle icon at the top-right of the cell)Click the filter icon for sales column
  5. Hover the cursor over the Number Filters option. This will show you all the number related filter options in Excel.
  6. Click on the ‘Less than’ option.Click on Less then option in number filters
  7. In the ‘Custom Autofilter’ dialog box that opens, enter the value ‘200’ in the fieldEnter the number filter value in the dialog box
  8. Click OK. This will filter and show only those records where the sales value is less than 200
  9. Select all the filtered records
  10. Right-click on any of the cells and click on Delete RowClick on Delete Row Option for filtered row based on numbers
  11. In the dialog box that opens, click on OK. At this point, you will see no records in the dataset.Click OK to delete rows based on the value
  12. Click the Data tab and click on the Filter icon. This will remove the filter and you will see all the records except the deleted ones.

There are many number filters that you can use in Excel – such as less than/greater than, equal/does not equal, between, top 10, above or below average, etc.

Note: You can use multiple filters as well. For example, you can delete all the rows where the sales value is greater than 200 but less than 500. In this case, you need to use two filter conditions. The Custom Autofilter dialog box allows having two filter criteria (AND as well as OR).

Just like the number filters, you can also filter the records based on the date. For example, if you want to remove all the records of the first quarter, you can do that by using the same steps above. When you’re working with Date filters, Excel automatically shows you relevant filters (as shown below).

Date Filter in Excel

While filtering is a great way to quickly delete rows based on a value or a condition, it has one drawback – it deletes the entire row. For example, in the below case, it would delete all the data which is to the right of the filtered dataset.

What if I only want to delete records from the dataset, but want to keep the remaining data intact.

You can’t do that with filtering, but you can do that with sorting.

Sort the Dataset and Then Delete the Rows

Although sorting is another way to delete rows based on value, but in most cases, you’re better off using the filter method covered above.

This sorting technique is recommended only when you want to delete the cells with the values and not the entire rows.

Suppose you have a dataset as shown below and you want to delete all the records where the region is Mid-west.

Below are the steps to do this using sorting:

  1. Select any cell in the data
  2. Click on the Data tab
  3. In the Sort & Filter group, click on the Sort icon.Click the Sort Icon in the data tab in Ribbon
  4. In the Sort dialog box that opens, select Region in the sort by column.Select Region as the basis to sort and delete rows1
  5. In the Sort on option, make sure Cell Values is selectedMake Sure Cells Values is selected as the basis to sort the data
  6. In Order option, select A to Z (or Z to A, doesn’t really matter).
  7. Click OK. This will give you the sorted data set as shown below (sorted by column B).Data After Sorting
  8. Select all the records with the region Mid-West (all the cells in the rows, not just the region column)
  9. Once selected, right-click and then click on Delete. This will open the Delete dialog box.Right and click on Delete after selecting all the cells of the records you want to delete
  10. Make sure the ‘Shift cells up’ option is selected.Click on Shift Cells Up option
  11. Click OK.

The above steps would delete all the records where the region was Mid-West, but it doesn’t delete the entire row. So, if you have any data on the right or left of your dataset, it will remain unharmed.

In the above example, I have sorted the data based on the cell value, but you can also use the same steps to sort based on numbers, dates, cell color or font color, etc.

Here is a detailed guide on how to sort data in Excel

In case you want to keep the original data set order but remove the records based on criteria, you need to have a way to sort the data back to the original one. To do this, add a column with serial numbers before sorting the data. Once you’re done with deleting the rows/records, simply sort based using this extra column you added.

Find and Select the Cells Based on Cell Value and Then Delete the Rows

Excel has a Find and Replace functionality that can be great when you want to find and select cells with a specific value.

Once you have selected these cells, you can easily delete the rows.

Suppose you have the dataset as shown below and you want to delete all the rows where the region is Mid-West.

Below are the steps to do this:

  1. Select the entire dataset
  2. Click the Home tabClick the Home Tab in the Ribbon
  3. In the Editing group, click on the ‘Find & Select’ option and then click on Find (you can also use the keyboard shortcut Control + F).Click on FIND
  4. In the Find and Replace dialog box, enter the text ‘Mid-West’ in the ‘Find what:’ field.Enter the region in find what field
  5. Click on Find All. This will instantly show you all the instances of the text Mid-West that Excel was able to find.Click on Find All
  6. Use the keyboard shortcut Control + A to select all the cells that Excel found. You will also be able to see all the selected cells in the dataset.Select all the records where the search term is found
  7. Right-click on any of the selected cells and click on Delete. This will open the Delete dialog box.Delete all the rows based on the term that was searched for
  8. Select the ‘Entire row’ optionClick on Entire row to delete all selected rows
  9. Click OK.

The above steps would delete all the cells where the region value is Mid-west.

Note: Since Find and Replace can handle wild card characters, you can use these when finding data in Excel. For example, if you want to delete all the rows where the region is either Mid-West or South-West, you can use ‘*West‘ as the text to find in the Find and Replace dialog box. This will give you all the cells where the text ends with the word West.

Delete All Rows With a Blank Cell

In case you want to delete all the rows where there are blank cells, you can easily do this with an inbuilt functionality in Excel.

It’s the Go-To Special Cells option – which allows you to quickly select all the blank cells. And once you have selected all the blank cells, deleting these is super simple.

Suppose you have the dataset as shown below and I want to delete all the rows where I don’t have the sale value.

Below are the steps to do this:

  1. Select the entire dataset (A1:D16 in this case).
  2. Press the F5 key. This will open the ‘Go To’ dialog box (You can also get this dialog box from Home –> Editing –> Find and Select –> Go To).
  3. In the ‘Go To’ dialog box, click on the Special button. This will open the ‘Go To Special’ dialog boxClick on Special button in Go To dialog box
  4. In the Go To Special dialog box, select ‘Blanks’.Select Blanks option to select all blank cells
  5. Click OK.

The above steps would select all the cells that are blank in the dataset.

Once you have the blank cells selected, right-click on any of the cells and click on Delete.

Click on delete to remove all rows with blank cells

In the Delete dialog box, select the ‘Entire row’ option and click OK. This will delete all rows that have blank cells in it.

Click on Entire row to delete all selected rows

If you’re interested in learning more about this technique, I wrote a detailed tutorial on how to delete rows with blank cells. It includes the ‘Go To Special’ method as well as a VBA method to delete rows with blank cells.

Filter and Delete Rows Based On Cell Value (using VBA)

The last method that I am going to show you include a little bit of VBA.

You can use this method if you often need to delete rows based on a specific value in a column. You can add the VBA code once and add it your Personal Macro workbook. This way it will be available for use in all of your Excel workbooks.

This code works the same way as the Filter method covered above (except the fact that this does all the steps in the backend and save you some clicks).

Suppose you have the dataset as shown below and you want to delete all the rows where the region is Mid-West.

Below is the VBA code that will do this.

Sub DeleteRowsWithSpecificText()
'Source:https://trumpexcel.com/delete-rows-based-on-cell-value/
ActiveCell.AutoFilter Field:=2, Criteria1:="Mid-West"
ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete
End Sub

The above code uses the VBA Autofilter method to first filter the rows based on the specified criteria (which is ‘Mid-West’), then select all the filtered rows and delete it.

Note that I have used Offset in the above code to make sure my header row is not deleted.

The above code doesn’t work if your data is in an Excel Table. The reason for this is that Excel considers an Excel Table as a list object. So if you want to delete rows that are in a Table, you need to modify the code a bit (covered later in this tutorial).

Before deleting the rows, it will show you a prompt as shown below. I find this useful as it allows me to double-check the filtered row before deleting.

Remember that when you delete rows using VBA, you can’t undo this change. So use this only when you’re sure that this works the way you want. Also, it’s a good idea to keep a backup copy of the data just in case anything goes wrong.

In case your data is in an Excel Table, use the below code to delete rows with a specific value in it:

Sub DeleteRowsinTables()
'Source:https://trumpexcel.com/delete-rows-based-on-cell-value/
Dim Tbl As ListObject
Set Tbl = ActiveSheet.ListObjects(1)
ActiveCell.AutoFilter Field:=2, Criteria1:="Mid-West"
Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
End Sub

Since VBA considers Excel Table as a List object (and not a range), I had to change the code accordingly.

Where to Put the VBA code?

This code needs to be placed in the VB Editor backend in a module.

Below are the steps that will show you how to do this:

  1. Open the workbook in which you want to add this code.
  2. Use the keyboard shortcut ALT + F11 to open the VBA Editor window.
  3. In this VBA Editor window, on the left, there is a ‘Project Explorer’ pane (which lists all the workbooks and worksheets objects). Right-click on any object in the workbook (in which you want this code to work), hover the cursor over ‘Insert’ and then click on ‘Module’. This will add the Module object to the Workbook and also open the Module code window on the right
  4. In the module window (that will appear on the right), copy and paste the above code.

Once you have the code in the VB Editor, you can run the code by using any of the below methods (make sure you have the selected any cell in the dataset on which you want to run this code):

  1. Select any line within the code and hit the F5 key.
  2. Click on the Run button in the toolbar in the VB Editor
  3. Assign the macro to a button or a shape and run it by clicking on it in the worksheet itself
  4. Add it to the Quick Access Toolbar and run the code with a single click.

You can read all about how to run the macro code in Excel in this article.

Note: Since the workbook contains a VBA macro code, you need to save it in the macro-enabled format (xlsm).

You may also like the following Excel tutorials:

  • How to Delete Every Other Row in Excel
  • How to Delete a Pivot Table in Excel
  • How to Delete Every Other Row in Excel
  • How to Move Rows and Columns in Excel (The Best and Fastest Way)
  • Highlight Rows Based on a Cell Value in Excel (Conditional Formatting)
  • How to Insert Multiple Rows in Excel
  • Number Rows in Excel
  • How to Quickly Unhide COLUMNS in Excel
  • Hide Zero Values in Excel

Bottom Line: Learn how to use VBA macros to delete rows based on cell values or conditions. Includes video tutorial and sample code.

Skill Level: Intermediate

Video Tutorial

Watch on YouTube & Subscribe to our Channel

Download the Excel File

Here is the file I use in the video above that contains the VBA macro code examples.

Does your data preparation process include deleting the same rows based on a condition? If so, you can use a macro to instantly delete any rows that have a particular value, date, or even blank cells.

The overall process is two simple steps:

  1. The first step is to filter the rows based on filter criteria for the values to be deleted.
  2. Then the macro deletes the visible cells in the range.

The Process Explained

Below is an image of a data set that has some blank cells in column E (Product). You can see one of those blank cells is E6.

Delete blank cells using a macro

To remove the rows that have blank cells like this one, the macro first applies a filter to the product column.

Next, the macro simply deletes all of the visible rows that have been left by the filter. It uses the SpecialCells method to create a reference to the visible cells.

This is the same as using the Go To Special menu (keyboard shortcut Alt+;) to select blanks. Checkout my article and video on how to copy & paste visible cells to learn more.

Rows with blank cells deleted

Finally, the macro can also clear the filters so that you are left viewing the entire data range, minus the rows you’ve deleted.

New data set with blank rows deleted

It’s a simple 2 or 3 step macro that will save time from doing this process manually.

Important Note: I added a step to clear all filters in the range or Table at the beginning of the macro. This ensures that there are no filters applied to other columns that could cause additional rows to be filtered out.

A big thanks to Hoang for pointing this out on the YouTube video!

The VBA Macro Code

The VBA code below can be copy/pasted to the VB Editor. The code is also included in the Excel file in the Download section above.

Sub Delete_Rows_Based_On_Value()
'Apply a filter to a Range and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/

Dim ws As Worksheet

  'Set reference to the sheet in the workbook.
  Set ws = ThisWorkbook.Worksheets("Regular Range")
  ws.Activate 'not required but allows user to view sheet if warning message appears

    'Clear any existing filters
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0

  '1. Apply Filter
  ws.Range("B3:G1000").AutoFilter Field:=4, Criteria1:=""

    '2. Delete Rows
  Application.DisplayAlerts = False
    ws.Range("B4:G1000").SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True

    '3. Clear Filter
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0

End Sub

You will just need to update the Worksheet and Range references for your specific file.

Avoiding the Pop-up Warning

When you run the macro you will receive a pop-up warning message that says “Delete entire sheet row?”

If you want to run the macro without having that pop-up box interrupting, you can simply remove the apostrophes before the two lines of code that begin with Application.DisplayAlerts.

So that portion of the macro now looks like this:

  '2. Delete Rows
  Application.DisplayAlerts = False
    ws.Range("B4:G1000").SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True

The Application.DisplayAlerts property is a toggle to turn warning alerts on/off. Setting it to False will turn off the alerts. These are alerts you might see from Excel when deleting ranges, deleting sheets, closing without saving, etc.

Applying the Macro to Tables

If your data is in an Excel Table instead of just a range of cells, you can still delete rows based on cell contents using a macro. The code is almost the same, but tweaked slightly so that it applies to Tables. Here is the code you would use for a Table.

Sub Delete_Rows_Based_On_Value_Table()
'Apply a filter to a Table and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/

Dim lo As ListObject

  'Set reference to the sheet and Table.
  Set lo = Sheet3.ListObjects(1)
  ws.Activate

    'Clear any existing filters
  lo.AutoFilter.ShowAllData

    '1. Apply Filter
  lo.Range.AutoFilter Field:=4, Criteria1:="Product 2"

    '2. Delete Rows
  Application.DisplayAlerts = False
    lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True

  '3. Clear Filter
  lo.AutoFilter.ShowAllData

End Sub

Additional Macros

I’ve also included a few additional macros to help customize the process further.

Custom Warning Message

You can create a custom message box before deleting rows, instead of the default Excel warning to delete rows. The macro below also tells you the number of rows it is going to delete, and asks if you want to proceed.

Sub Delete_Rows_Based_On_Value_Table_Message()
'Display Yes/No message prompt before deleting rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/

Dim lo As ListObject
Dim lRows As Long
Dim vbAnswer As VbMsgBoxResult

  'Set reference to the sheet and Table.
  Set lo = Sheet6.ListObjects(1)
  lo.Parent.Activate 'Activate sheet that Table is on.

    'Clear any existing filters
  lo.AutoFilter.ShowAllData

    '1. Apply Filter
  lo.Range.AutoFilter Field:=4, Criteria1:="Product 2"

    'Count Rows & display message
  On Error Resume Next
    lRows = WorksheetFunction.Subtotal(103, lo.ListColumns(1).DataBodyRange.SpecialCells(xlCellTypeVisible))
  On Error GoTo 0

    vbAnswer = MsgBox(lRows & " Rows will be deleted.  Do you want to continue?", vbYesNo, "Delete Rows Macro")

    If vbAnswer = vbYes Then

        'Delete Rows
    Application.DisplayAlerts = False
      lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True

      'Clear Filter
    lo.AutoFilter.ShowAllData

      End If

End Sub

This is what the custom pop-up box looks like:

Delete Rows Macro Custom Message

This pop-up just serves as a check before deleting rows. It’s a great way to double check everything looks good before deleting. It also prevents you or your users from accidentally running the macro.

Checkout my video on how to add a Yes/No message box before the macro runs for details on this code. It’s part of my 4 part video series on the Personal Macro Workbook.

Deleting Rows Based on Multiple Criteria

You can also delete rows with a macro using more than one criteria. In the video above, I filter for rows that have both a blank Product field and a date before 1/1/2015.

Sub Delete_Rows_Based_On_Multiple_Values()
'Apply a filter to a Table and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/

Dim lo As ListObject

  'Set reference to the sheet and Table.
  Set lo = Sheet5.ListObjects(1)
  lo.Parent.Activate 'Activate sheet that Table is on.

    'Clear any existing filters
  lo.AutoFilter.ShowAllData

    '1. Apply Filter - Blanks in Product for before 2015 only
  lo.Range.AutoFilter Field:=4, Criteria1:=""
  lo.Range.AutoFilter Field:=1, Criteria1:="<1/1/2015"

    '2. Delete Rows
  Application.DisplayAlerts = False
    lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True

  '3. Clear Filter
  lo.AutoFilter.ShowAllData

End Sub

This allows you to delete rows based on values in multiple columns. The filtering essentially uses AND logic between the columns and all conditions must be met.

If you want to use OR logic where any of the conditions are met, you can run multiple macros, or create a formula in a helper column with the logic.

Depending on the logic, it might be easiest to just create two separate macros. You can put the code all in one macro, or create another macro to call each macro. Here’s an example of what that macro might look like.

Sub Delete_Rows()

  Call Delete_Blank_Rows
  Call Delete_Before_2015

End Sub

This method also allows you to run each macro individually.

Delete Rows Based on Criteria Specified by User

Daniel and Bob asked a great question in the comments below. They want the user of the file to be able to specify the filter criteria, instead of having it hard-coded in the macro.

We can use the Application.InputBox method to ask the user to input the criteria.

Input Box to Prompt User for Filter Criteria for Delete Rows Macro

The InputBox method creates a pop-up message box that allows the user to type in the text box. The value in the text box is passed back to the macro and stored in a variable.

Here is the VBA code. I also added it to the sample file in the downloads section above.

Sub Delete_Rows_User_Input()
'Display Yes/No message prompt before deleting rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/

Dim lo As ListObject
Dim lRows As Long
Dim vbAnswer As VbMsgBoxResult
Dim sCriteria As Variant

  'Set reference to the sheet and Table.
  Set lo = Sheet9.ListObjects(1)
  lo.Parent.Activate 'Activate sheet that Table is on.

    'Clear any existing filters
  lo.AutoFilter.ShowAllData

    'Ask user for input
  sCriteria = Application.InputBox(Prompt:="Please enter the filter criteria for the Product column." _
                                    & vbNewLine & "Leave the box empty to filter for blanks.", _
                                    Title:="Filter Criteria", _
                                    Type:=2)

    'Exit if user presses Cancel button
  If sCriteria = False Then Exit Sub

    '1. Apply Filter
  lo.Range.AutoFilter Field:=4, Criteria1:=sCriteria

    'Count Rows & display message
  On Error Resume Next
    lRows = WorksheetFunction.Subtotal(103, lo.ListColumns(1).DataBodyRange.SpecialCells(xlCellTypeVisible))
  On Error GoTo 0

    vbAnswer = MsgBox(lRows & " Rows will be deleted.  Do you want to continue?", vbYesNo, "Delete Rows Macro")

    If vbAnswer = vbYes Then

        'Delete Rows
    Application.DisplayAlerts = False
      lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True

      'Clear Filter
    lo.AutoFilter.ShowAllData

      End If

End Sub

Pre-fill the User’s Name in the Input Box

Daniel also had a question about setting the filter criteria to the current user’s name. The Application.UserName property actually returns this value as long as the user has set it properly in Office/Excel.

Input Box to Prompt Default to User Name

We can use this as the Default parameter value in the input box.

  sCriteria = Application.InputBox(Prompt:="Please enter the filter criteria for the Product column." _
                                    & vbNewLine & "Leave the box empty to filter for blanks.", _
                                    Title:="Filter Criteria", _
                                    Default:=Application.UserName, _
                                    Type:=2)

The user might still need to change the name if it does not match the value in the worksheet column you are filtering, but you can also get that synced up with the user to save them a lot of time.

Related Topics

Check out my posts on these similar topics as well.

  • The Ultimate Guide to Filters with VBA Macros
  • How to Copy and Paste Visible Cells Only
  • 3 Ways to Delete Entire Blank Rows
  • How to Create Your Personal Macro Workbook

Conclusion

Macros like this can be especially helpful if you are looking to clean up data and get rid of entries that you know you don’t need–maybe because they are too old or they are only partially complete.

There are a lot of ways to accomplish this task. Another common approach is to loop through cells and check their values individually.

However, I typically use this approach because it can be a lot faster to use the built in filters in Excel. It also allows us to preview the range before we delete it.

What will you use these macros for? Please leave a comment below with any suggestions and questions. Thank you! 🙂

In this VBA Tutorial, you learn how to use Excel VBA to delete rows based on a variety of criteria.Excel VBA Tutorial about how to delete rows with macros

This VBA Tutorial is accompanied by Excel workbooks containing the data and macros I use in the examples below. You can get immediate free access to these example workbooks by subscribing to the Power Spreadsheets Newsletter.

Use the following Table of Contents to navigate to the section you’re interested in.

Related VBA and Macro Tutorials

The following VBA and Macro Tutorials may help you better understand and implement the contents below.

  • General VBA constructs and structures:
    • Learn about using variables here.
    • Learn about VBA data types here.
    • Learn about R1C1 style-references here.
    • Learn about using worksheet functions in VBA here.
  • Practical VBA applications and macro examples:
    • Learn how to work with worksheets here.
    • Learn how to insert rows here.
    • Learn how to delete columns here.
    • Learn how to find the last column in a worksheet here.

You can find additional VBA and Macro Tutorials in the Archives.

VBA Code to Delete a Row

To delete a row using VBA, use a statement with the following structure:

Worksheets.Rows(Row#).Delete

Process Followed by VBA Code

Identify row to delete > Delete row

VBA Statement Explanation

Worksheets.Rows(Row#).Delete

  1. Item: Worksheets.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.
  2. Item: Rows(Row#).
    • VBA Construct: Worksheet.Rows property.
    • Description: Returns a Range object representing row number Row# of the worksheet returned by item #1 above.

      If you explicitly declare a variable to represent Row#, use the Long data type.

  3. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #2 above.

Macro Example

The following macro deletes row 6 of the worksheet named “Delete row”.

Sub deleteRow()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/
 
    Worksheets("Delete row").Rows(6).Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes row 6 of the worksheet.

Macro deletes row 6

#2: Delete a Row and Shift Up

VBA Code to Delete a Row and Shift Up

To delete a row and explicitly shift cells up to replace the deleted row, use a statement with the following structure:

Worksheet.Rows(Row#).Delete Shift:=xlShiftUp

Process Followed by VBA Code

Identify row to delete > Delete row and shift cells up

VBA Statement Explanation

Worksheet.Rows(Row#).Delete Shift:=xlShiftUp

  1. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.
  2. Item: Rows(Row#).
    • VBA Construct: Worksheet.Rows property.
    • Description: Returns a Range object representing row number Row# of the worksheet returned by item #1 above.

      If you explicitly declare a variable to represent Row#, use the Long data type.

  3. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #2 above.
  4. Item: Shift:=xlShiftUp.
    • VBA Construct: Shift parameter of the Range.Delete method.
    • Description:
      • Shifts rows up (xlShiftUp) to replace the deleted row.
      • You can usually omit this parameter. By default, VBA decides how to shift the cells based on the range’s shape. When deleting a row, this generally results in Excel shifting the cells up.

Macro Example

The following macro deletes row 10 of the worksheet named “Delete row” and explicitly specifies to shift cells up to replace the deleted row.

Sub deleteRowShiftUp()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/
 
    Worksheets("Delete row").Rows(10).Delete Shift:=xlShiftUp

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes row 10 of the worksheet and shifts cells up to replace the deleted row.

Macro deletes row 10 and shift cells up

#3: Delete Multiple Rows

VBA Code to Delete Multiple Rows

To delete multiple rows, use a statement with the following structure:

Worksheet.Rows("FirstRow#:LastRow#").Delete

Process Followed by VBA Code

Identify rows to delete > Delete rows

VBA Statement Explanation

Worksheet.Rows(“FirstRow#:LastRow#”).Delete

  1. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.
  2. Item: Rows(“FirstRow#:LastRow#”).
    • VBA Construct: Worksheet.Rows property.
    • Description: Returns a Range object representing rows number FirstRow# through LastRow# of the worksheet returned by item #1 above.
  3. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #2 above.

Macro Example

The following macro deletes rows 14 to 18 of the worksheet named “Delete row”.

Sub deleteMultipleRows()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/
 
    Worksheets("Delete row").Rows("14:18").Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes rows 14 to 18 of the worksheet.

Macro deletes rows 14 to 18

#4: Delete Selected Row

VBA Code to Delete Selected Row

To delete the selected row (the row containing the active cell), use the following statement:

ActiveCell.EntireRow.Delete

Process Followed by VBA Code

Identify active cell > Return entire row > Delete row

VBA Statement Explanation

ActiveCell.EntireRow.Delete

  1. Item: ActiveCell.
    • VBA Construct: Application.ActiveCell property.
    • Description: Returns a Range object representing the active cell.
  2. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire row containing the cell range returned by item #1 above.
  3. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #2 above.

Macro Example

The following macro deletes the selected row (the row containing the active cell):

Sub deleteSelectedRow()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/
 
    ActiveCell.EntireRow.Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. When I execute the macro, the active cell is B20. As expected, Excel deletes the selected row.

Macro deletes selected row

#5: Delete Multiple Selected Rows

VBA Code to Delete Multiple Selected Rows

To delete multiple selected rows, use the following statement:

Selection.EntireRow.Delete

Process Followed by VBA Code

Identify selected cells > Return entire rows > Delete rows

VBA Statement Explanation

Selection.EntireRow.Delete

  1. Item: Selection.
    • VBA Construct: Application.Selection property.
    • Description: Returns a Range object representing the current cell range selection.
  2. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire row containing the Range object returned by item #1 above.
  3. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #2 above.

Macro Example

The following macro deletes the (multiple) selected rows.

Sub deleteSelectedRows()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/
 
    Selection.EntireRow.Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. When I execute the macro, the selected cells are B24 to B28. As expected, Excel deletes the selected rows.

Macro deletes selected rows

#6: Delete Blank Rows

VBA Code to Delete Blank Rows

To delete blank rows, use a macro with the following statement structure:

With Worksheet
    For Counter = LastRow To FirstRow Step -1
        If WorksheetFunction.CountA(.Rows(Counter)) = 0 Then
            If Not BlankRows Is Nothing Then
                Set BlankRows = Union(BlankRows, .Rows(Counter))
            Else
                Set BlankRows = .Rows(Counter)
            End If
        End If
    Next Counter
End With
If Not BlankRows Is Nothing Then BlankRows.Delete

Process Followed by VBA Code

Loop through all rows > Is row empty? > Add row to variable representing empty rows > Delete rows

VBA Statement Explanation

Lines #1 and #11: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: Statements within the With… End With statement (lines #2 through #10 below) are executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Lines #2 and #10: For Counter = LastRow To FirstRow Step -1 | Next Counter

  1. Item: For… Next Counter.
    • VBA Construct: For… Next statement.
    • Description: Repeats the statements within the loop (lines #3 through #9 below) for each row between (and including) FirstRow (item #4 below) and LastRow (item #3 below).
  2. Item: Counter.
    • VBA Construct: Counter of For… Next statement.
    • Description: Loop counter. If you explicitly declare a variable to represent the loop counter, use the Long data type.
  3. Item: LastRow.
    • VBA Construct: Counter Start of For… Next statement.
    • Description: Number of the last row (further down the worksheet) you want the macro to consider when identifying blank rows. The number of the last row is also the initial value of Counter (item #2 above).

      If you explicitly declare a variable to represent the number of the last row to consider, use the Long data type.

  4. Item: FirstRow.
    • VBA Construct: Counter End of For… Next statement.
    • Description: Number of the first row (closer to the top of the worksheet) you want the macro to consider when identifying blank rows. The number of the first row is also the final value of Counter (item (#2 above).

      If you explicitly declare a variable to represent the number of the first row to consider, use the Long data type.

  5. Item: Step -1.
    • VBA Construct: Step of For… Next statement.
    • Description: Amount by which Counter (item #2 above) changes every time a loop iteration occurs.

      In this scenario, you loop backwards: from LastRow (item #3 above) to FirstRow (item #4 above). Therefore, step is -1.

Line #3: If WorksheetFunction.CountA(.Rows(Counter)) = 0 Then

  1. Item: If… Then.
    • VBA Construct: Opening line of If… Then… Else statement.
    • Description: Conditionally executes the statements within the If… Then block (lines #4 through #8 below) if the condition specified by item #4 below is met.
  2. Item: WorksheetFunction.CountA.
    • VBA Construct: WorksheetFunction.CountA method.
    • Description: Counts the number of cells that aren’t empty in the range returned by item #3 below.

      Since the range returned by item #3 below represents the row through which the macro is currently looping, Worksheet.CountA counts the number of cells that aren’t empty in that row.

  3. Item: .Rows(Counter).
    • VBA Construct: Worksheet.Rows property.
    • Description: Returns a Range object representing the row through which the macro is currently looping.
  4. Item: WorksheetFunction.CountA(.Rows(Counter)) = 0.
    • VBA Construct: Condition of If… Then… Else statement.
    • Description: This condition is a numeric expression that evaluates to True or False, as follows:
      • True: When the WorksheetFunction.CountA method (item #2 above) returns 0. This happens when the row through which the macro is currently looping (item #3 above) is empty and, therefore, the number of non-empty cells is 0.
      • False: When WorksheetFunction.CountA returns a number other than 0. This happens when the row through which the macro is currently looping isn’t empty and, therefore, the number of non-empty cells isn’t 0.

Line #4: If Not BlankRows Is Nothing Then

  1. Item: If… Then.
    • VBA Construct: Opening line of If… Then… Else statement.
    • Description: Conditionally executes the statement within the If… Then… Else block (line #5 below) if the condition specified by item #6 below is met.
  2. Item: Not.
    • VBA Construct: Not operator.
    • Description: Carries out a logical negation on item #3 below. In other words, if item #3 returns:
      • True, the result is False.
      • False, the result is True.
  3. Item: BlankRows.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the empty rows found by the macro.
  4. Item: Is.
    • VBA Construct: Is Operator.
    • Description: Compares 2 object reference variables: (i) Not BlankRows (items #2 and #3 above) vs. (ii) Nothing (item #5 below).

      If both object references refer to the same object, the Is operator returns True. If they refer to different objects, Is returns False.

  5. Item: Nothing.
    • Description: The default value for a data type. In the case of an object variable (such as BlankRows), a null reference.
  6. Item: Not BlankRows Is Nothing.
    • VBA Construct: Condition of If… Then… Else statement.
    • Description: The condition is an expression that evaluates to True or False, as follows:
      • True: When “Not BlankRows” (items #2 and #3 above) refers to the same object as Nothing (item #5 above). This happens when BlankRows is “something”.

        Since BlankRows holds a Range object representing cell ranges within the empty rows found by the macro, BlankRows is something if the macro finds at least one such row.

      • False: When “Not BlankRows” refers to a different object from Nothing. This happens when BlankRows itself is Nothing. This occurs prior to the macro finding the first empty row. This is because BlankRows isn’t assigned to anything prior to that moment.

Line #5: Set BlankRows = Union(BlankRows, .Rows(Counter))

  1. Item: Set… =.
    • VBA Construct: Set statement.
    • Description: Assigns the object reference returned by item #6 below to BlankRows (item #2 below).
  2. Item: BlankRows.
    • VBA Construct: Object (Range) variable of Set statement.
    • Description:
      • Holds a Range object representing the empty rows found by the macro.
      • BlankRows is included twice in the statement. In the first mention (Set BlankRows), BlankRows is the object variable to which an object reference is assigned.
  3. Item: Union.
    • VBA Construct: Application.Union method.
    • Description: Returns a Range object representing the union of the Range objects returned by items #4 and #5 below.
  4. Item: BlankRows.
    • VBA Construct: Object (Range) variable.
    • Description:
      • Holds a Range object representing the empty rows found by the macro.
      • BlankRows is included twice in the statement. In the second mention (Union(BlankRows, .Rows(Counter)), BlankRows is one of the parameters of the Application.Union method.
  5. Item: .Rows(Counter).
    • VBA Construct: Worksheet.Rows property.
    • Description: Returns a Range object representing the row through which the macro is currently looping.
  6. Item: Union(BlankRows, .Rows(Counter).
    • VBA Construct: Object expression of Set statement.
    • Description: Returns the new Range object reference assigned to the BlankRows object variable (item #2 above). This is the union of the following 2 Range objects:
      • Prior to the Set statement, BlankRows represents cell ranges within the empty rows found by the macro prior to the row through which it’s currently looping.
      • “.Rows(Counter)” represents the row through which the macro is currently looping.

      Graphically, this looks as follows:

      Union(BlankRows, .Rows(Counter))

      In other words, any empty row the macro finds is “added” to BlankRows.

Line #6: Else

  1. Item: Else.
    • VBA Construct: Else clause of If… Then… Else statement.
    • Description: The statement following the Else clause (line #7 below) is executed if the condition tested in the opening line of the If… Then… Else statement (line #4 above) isn’t met and returns False.

Line #7: Set BlankRows = .Rows(Counter)

  1. Item: Set… =.
    • VBA Construct: Set statement.
    • Description: Assigns the object reference returned by item #3 below to BlankRows (item #2 below).
  2. Item: BlankRows.
    • VBA Construct: Object (Range) variable of Set statement.
    • Description: Holds a Range object representing the empty rows found by the macro.
  3. Item: .Rows(Counter).
    • VBA Construct: Worksheet.Rows property.
    • Description: Returns a Range object representing the row through which the macro is currently looping.

Lines #8 and #9: End If | End If

  1. Item: End If.
    • VBA Construct: Closing lines of If… Then… Else statements.
    • Description: Ends the If… Then… Else statements that began in lines #3 and #4 above.

Line #12: If Not BlankRows Is Nothing Then BlankRows.Delete

  1. Item: If… Then.
    • VBA Construct: If… Then… Else statement.
    • Description: Conditionally executes the statement at the end of the line (items #7 and #8 below) if the condition specified by item #6 below is met.
  2. Item: Not.
    • VBA Construct: Not operator.
    • Description: Carries out a logical negation on item #3 below. In other words, if item #3 returns:
      • True, the result is False.
      • False, the result is True.
  3. Item: BlankRows.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the empty rows found by the macro.
  4. Item: Is.
    • VBA Construct: Is Operator.
    • Description: Compares 2 object reference variables: (i) Not BlankRows (items #2 and #3 above) vs. (ii) Nothing (item #5 below).

      If both object references refer to the same object, the Is operator returns True. If they refer to different objects, Is returns False.

  5. Item: Nothing.
    • Description: The default value for a data type. In the case of an object variable (such as BlankRows), a null reference.
  6. Item: Not BlankRows Is Nothing.
    • VBA Construct: Condition of If… Then… Else statement.
    • Description: The condition is an expression that evaluates to True or False, as follows:
      • True: When “Not BlankRows” (items #2 and #3 above) refers to the same object as Nothing (item #5 above). This happens when BlankRows is “something”.

        Since BlankRows holds a Range object representing cell ranges within the empty rows found by the macro, BlankRows is something if the macro has found at least 1 empty row.

      • False: When “Not BlankRows” refers to a different object from Nothing. This happens when BlankRows itself is Nothing. This, in turn, occurs when the macro founds no empty rows.
  7. Item: BlankRows.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the empty rows found by the macro.
  8. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #7 above.

Macro Example

The following macro deletes all blank rows between rows number myFirstRow and LastRow.

  • myFirstRow is set to 6.
  • myLastRow is set to the number of the last row with data in the worksheet named “Delete empty rows”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
Sub deleteEmptyRows()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myWorksheet As Worksheet
    Dim iCounter As Long
    Dim myBlankRows As Range

    myFirstRow = 6

    Set myWorksheet = Worksheets("Delete empty rows")

    With myWorksheet
        myLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For iCounter = myLastRow To myFirstRow Step -1
            If WorksheetFunction.CountA(.Rows(iCounter)) = 0 Then
                If Not myBlankRows Is Nothing Then
                    Set myBlankRows = Union(myBlankRows, .Rows(iCounter))
                Else
                    Set myBlankRows = .Rows(iCounter)
                End If
            End If
        Next iCounter
    End With

    If Not myBlankRows Is Nothing Then myBlankRows.Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes all blank rows between row 6 and the last row with data on the worksheet.

Macro deletes blank rows

#7: Delete Rows with Blank Cells

VBA Code to Delete Rows with Blank Cells

To delete rows with blank cells using VBA, use a macro with the following statement structure:

With Worksheet
    Set RangeForCriteria = .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))
End With
On Error Resume Next
RangeForCriteria.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

Process Followed by VBA Code

Identify cell range > Identify empty cells > Return entire rows > Delete rows

VBA Statement Explanation

Lines #1 and #3: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: The statement within the With… End With statement (line #2 below) is executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Line #2: Set RangeForCriteria = .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))

  1. Item: Set… =.
    • VBA Construct: Set statement.
    • Description: Assigns the object reference returned by items #3 through #5 below to RangeForCriteria (item #2 below).
  2. Item: RangeForCriteria.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cell range you want the macro to search for blank cells.
  3. Item: .Range.
    • VBA Construct: Worksheet.Range property.
    • Description: Returns a Range object representing a cell range specified as follows:
      • Upper-left corner cell: Range object returned by item #4 below.
      • Lower-right corner cell: Range object returned by item #5 below.
  4. Item: .Cells(FirstRow, FirstColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number FirstRow and column number FirstColumn.

      FirstRow and FirstColumn are the number of, respectively, the first row and first column in the cell range you want the macro to search for blank cells. If you explicitly declare a variable to represent FirstRow or FirstColumn, use the Long data type.

  5. Item: .Cells(LastRow, LastColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number LastRow and column number LastColumn.

      LastRow and LastColumn are the number of, respectively, the last row and last column in the cell range you want the macro to search for blank cells. If you explicitly declare a variable to represent LastRow or LastColumn, use the Long data type.

Line #4: On Error Resume Next

  1. Item: On Error Resume Next.
    • VBA Construct: On Error Resume Next statement.
    • Description: Specifies that, when a run-time error occurs, control goes to the statement following the statement where the error occurs.

      The error-handler in this line #4 is necessary because, if the cell range you want the macro to search for blank cells doesn’t contain any such cells, line #5 below generates a run-time error.

Line #5: RangeForCriteria.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

  1. Item: RangeForCriteria.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cell range you want the macro to search for blank cells.
  2. Item: SpecialCells(xlCellTypeBlanks).
    • VBA Construct: Range.SpecialCells method and Type parameter of Range.SpecialCells method.
    • Description: Returns a Range object representing all empty cells within the cell range returned by RangeForCriteria (item #1 above).
  3. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire rows containing the Range object returned by item #2 above.
  4. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #3 above.

Macro Example

The following macro deletes all rows with blank cells between:

  • Rows number myFirstRow and myLastRow.
  • Columns number myFirstColumn and myLastColumn.

In this example:

  • myFirstRow is set to 6.
  • myFirstColumn is set to 2.
  • myLastRow is set to the number of the last row with data in the worksheet named “Delete row with blank cells”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
  • myLastColumn is set to the number of the last column with data in the same worksheet. The constructs used by the statement that finds the last column with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Column property.
Sub deleteRowBlankCells()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim myWorksheet As Worksheet
    Dim myRange As Range

    myFirstRow = 6
    myFirstColumn = 2

    Set myWorksheet = Worksheets("Delete row with blank cells")

    With myWorksheet
        With .Cells
            myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            myLastColumn = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        End With
        Set myRange = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
    End With

    On Error Resume Next
    myRange.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes all rows with blank cells between (i) row 6 and the last row with data on the worksheet, and (ii) column 2 and the last column with data on the worksheet.

Macro deletes rows with blank cells

#8: Delete Rows with Blank Cells in a Specific Column

VBA Code to Delete Rows with Blank Cells in a Specific Column

To delete rows with blank cells in a specific column using VBA, use a macro with the following statement structure:

With Worksheet
    With .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))
        .AutoFilter Field:=CriteriaField, Criteria1:="="
        On Error Resume Next
        .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count - 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With

Process Followed by VBA Code

Identify cell range > Filter to find blank cells > Exclude headers > Identify visible cells > Return entire rows > Delete rows

VBA Statement Explanation

Lines #1 and #8: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: Statements within the With… End With statement (lines #2 through #7 below) are executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Lines #2 and #6: With .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn)) | End With

  1. Item: With… End With.
    1. VBA Construct: With… End With statement.
    2. Description: Statements within the With… End With statement (lines #3 through #6 below) are executed on the range object returned by items #2 through #4 below.
  2. Item: .Range.
    • VBA Construct: Worksheet.Range property.
    • Description: Returns a Range object representing a cell range specified as follows:
      • Upper-left corner cell: Range object returned by item #3 below.
      • Lower-right corner cell: Range object returned by item #4 below.
  3. Item: .Cells(FirstRow, FirstColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number FirstRow and column number FirstColumn.

      FirstRow and FirstColumn are the number of, respectively, the first row and first column in the cell range you work with. If you explicitly declare a variable to represent FirstRow or FirstColumn, use the Long data type.

  4. Item: .Cells(LastRow, LastColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number LastRow and column number LastColumn.

      LastRow and LastColumn are the number of, respectively, the last row and last column in the cell range you work with. If you explicitly declare a variable to represent LastRow or LastColumn, use the Long data type.

Line #3: .AutoFilter Field:=CriteriaField, Criteria1:=”=”

  1. Item: .AutoFilter.
    • VBA Construct: Range.AutoFilter method.
    • Description: Filter the data within the range you work with using the AutoFilter and according to the parameters specified by items #2 and #3 below.
  2. Item: Field:=CriteriaField.
    • VBA Construct: Field parameter of Range.AutoFilter method.
    • Description: Specifies the field on which you want to base the filter. The leftmost field of the range you work with is Field 1. The rightmost field is the number of fields in the cell range you work with.

      If you explicitly declare a variable to represent CriteriaField, use the Long data type.

  3. Item: Criteria1:=”=”.
    • VBA Construct: Criteria1 parameter of Range.AutoFilter method.
    • Description: Specifies the filtering criteria. “=” finds blank cells.

Line #4: On Error Resume Next

  1. Item: On Error Resume Next.
    • VBA Construct: On Error Resume Next statement.
    • Description: Specifies that, when a run-time error occurs, control goes to the statement following the statement where the error occurs.

      The error-handler in this line #4 is necessary because, if the field you filter by (line #3 above) doesn’t contain blank cells, line #5 below generates a run-time error.

Line #5: .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count – 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete

  1. Item: Offset.
    • VBA Construct: Range.Offset property.
    • Description: Returns a Range object a number of rows above or below the cell range it works with, as returned by line #2 above.
  2. Item: RowOffset:=1.
    • VBA Construct: RowOffset parameter of Range.Offset property.
    • Description: Specifies that the cell range returned by Range.Offset (item #1 above) is 1 row below the range specified in line #2 above.

      Line #2 above specifies the cell range you work with. Therefore, the Range object that Range.Offset returns has the same size but is 1 row below the cell range you work with. This results in the following:

      • The headers of the cell range you work with are excluded from the Range object.
      • The first empty row below the last row with data (LastRow in line #2 above) is included. This extra line is handled by item #7 below.
  3. Item: Resize.
    • VBA Construct: Range.Resize property.
    • Description: Resizes the cell range returned by items #1 and #2 above.
  4. Item: RowSize.
    • VBA Construct: RowSize parameter of Range.Resize property.
    • Description: Specifies the number of rows in the new cell range returned by Range.Resize (item #3 above).
  5. Item: Rows.
    • VBA Construct: Range.Rows property.
    • Description: Returns a Range object representing the rows in the cell range it works with, as returned by line #2 above.
  6. Item: Count.
    • VBA Construct: Range.Count property.
    • Description: Returns the number of rows within the Range object returned by item #5 above.
  7. Item: Resize(RowSize:=(.Rows.Count – 1)).
    • VBA Construct: Range.Resize property.
    • Description: Resizes the cell range returned by items #1 and #2 above to reduce it by one row. The number of rows in the new range is obtained by subtracting 1 from the number of rows returned by line #2, as counted by items #5 and #6 above.

      This results in a cell range that excludes the first empty row below the last row with data that the Range.Offset property (items #1 and #2 above) included.

  8. Item: SpecialCells(xlCellTypeVisible).
    • VBA Construct: Range.SpecialCells method and Type parameter of Range.SpecialCells method.
    • Description: Returns a Range object representing all visible cells within the cell range you work with, excluding the headers (as required by item #2 above).

      Since line #3 above filters the data according to the criteria you specify, the visible cells returned by Range.SpecialCells are those containing blank cells in the column (field) you specify.

  9. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire rows containing the Range object returned by item #8 above.
  10. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #10 above.

Line #7: .AutoFilterMode = False

  1. Item: .AutoFilterMode = False.
    • VBA Construct: Worksheet.AutoFilterMode property.
    • Description: Specifies that the AutoFilter drop-down arrows aren’t displayed on the worksheet.

Macro Example

The following macro deletes all rows that meet the following conditions:

  • Are between:
    • Rows number (myFirstRow + 1) and myLastrow.
    • Columns number myFirstColumn and myLastColumn.
  • Contain a blank cell in field number myCriteriaField.

In this example:

  • myFirstRow is set to 5.
  • myFirstColumn is set to 2.
  • myCriteriaField is set to 1.
  • myLastRow is set to the number of the last row with data in the worksheet named “Delete row if cell is blank”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
  • myLastColumn is set to the number of the last column with data in the same worksheet. The constructs used by the statement that finds the last column with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Column property.
Sub deleteRowBlankCell()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim myCriteriaField As Long
    Dim myWorksheet As Worksheet

    myFirstRow = 5
    myFirstColumn = 2
    myCriteriaField = 1

    Set myWorksheet = Worksheets("Delete row if cell is blank")

    With myWorksheet
        With .Cells
            myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            myLastColumn = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        End With
        With .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
            .AutoFilter Field:=myCriteriaField, Criteria1:="="
            On Error Resume Next
            .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count - 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes the rows containing blank cells in myCriteriaField (1).

Macro deletes rows with blank cells in first column

#9: Delete Rows Containing Strings

VBA Code to Delete Rows Containing Strings

To delete rows containing strings using VBA, use a macro with the following statement structure:

With Worksheet
    Set RangeForCriteria = .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))
End With
On Error Resume Next
RangeForCriteria.SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete

Process Followed by VBA Code

Identify cell range > Identify cells with strings > Return entire rows > Delete rows

VBA Statement Explanation

Lines #1 and #3: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: The statement within the With… End With statement (line #2 below) is executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Line #2: Set RangeForCriteria = .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))

  1. Item: Set… =.
    • VBA Construct: Set statement.
    • Description: Assigns the object reference returned by items #3 through #5 below to RangeForCriteria (item #2 below).
  2. Item: RangeForCriteria.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cell range you want the macro to search for cells containing strings.
  3. Item: .Range.
    • VBA Construct: Worksheet.Range property.
    • Description: Returns a Range object representing a cell range specified as follows:
      • Upper-left corner cell: Range object returned by item #4 below.
      • Lower-right corner cell: Range object returned by item #5 below.
  4. Item: .Cells(FirstRow, FirstColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number FirstRow and column number FirstColumn.

      FirstRow and FirstColumn are the number of, respectively, the first row and first column in the cell range you want the macro to search for cells containing strings. If you explicitly declare a variable to represent FirstRow or FirstColumn, use the Long data type.

  5. Item: .Cells(LastRow, LastColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number LastRow and column number LastColumn.

      LastRow and LastColumn are the number of, respectively, the last row and last column in the cell range you want the macro to search for cells containing strings. If you explicitly declare a variable to represent LastRow or LastColumn, use the Long data type.

Line #4: On Error Resume Next

  1. Item: On Error Resume Next.
    • VBA Construct: On Error Resume Next statement.
    • Description: Specifies that, when a run-time error occurs, control goes to the statement following the statement where the error occurs.
      The error-handler in this line #4 is necessary because, if the cell range you want the macro to search for cells containing strings doesn’t contain any such cells, line #5 below generates a run-time error.

Line #5: RangeForCriteria.SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete

  1. Item: RangeForCriteria.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cell range you want the macro to search for cells containing strings.
  2. Item: SpecialCells(xlCellTypeConstants, xlTextValues).
    • VBA Construct: Range.SpecialCells method, Type and Value parameters of Range.SpecialCells method.
    • Description: Returns a Range object representing all cells containing constant (xlCellTypeConstants) text values (xlTextValues) within the cell range returned by RangeForCriteria (item #1 above). Those are the cells containing strings.
  3. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire rows containing the Range object returned by item #2 above.
  4. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #3 above.

Macro Example

The following macro deletes all rows containing strings between:

  1. Rows number myFirstRow and myLastRow.
  2. Columns number myFirstColumn and myLastColumn.

In this example:

  • myFirstRow is set to 6.
  • myFirstColumn is set to 2.
  • myLastRow is set to the number of the last row with data in the worksheet named “Delete rows containing strings”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
  • myLastColumn is set to the number of the last column with data in the same worksheet. The constructs used by the statement that finds the last column with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Column property.
Sub deleteRowContainingStrings()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim myWorksheet As Worksheet
    Dim myRange As Range

    myFirstRow = 6
    myFirstColumn = 2

    Set myWorksheet = Worksheets("Delete rows containing strings")

    With myWorksheet
        With .Cells
            myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            myLastColumn = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        End With
        Set myRange = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
    End With

    On Error Resume Next
    myRange.SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes all rows containing strings between (i) row 6 and the last row with data on the worksheet, and (ii) column 2 and the last column with data on the worksheet.

Macro deletes rows containing strings

#10: Delete Row Based on Cell Value

VBA Code to Delete Row Based on Cell Value

To delete rows based on the value in a specific cell using VBA, use a macro with the following statement structure:

With Worksheet
    With .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))
        .AutoFilter Field:=CriteriaField, Criteria1:=Value
        On Error Resume Next
        .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count - 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With

Process Followed by VBA Code

Identify cell range > Filter cells with value > Exclude headers > Identify visible cells > Return entire rows > Delete rows

VBA Statement Explanation

Lines #1 and #8: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: Statements within the With… End With statement (lines #2 through #7 below) are executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Lines #2 and #6: With .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn)) | End With

  1. Item: With… End With.
    1. VBA Construct: With… End With statement.
    2. Description: Statements within the With… End With statement (lines #3 through #6 below) are executed on the range object returned by items #2 through #4 below.
  2. Item: .Range.
    • VBA Construct: Worksheet.Range property.
    • Description: Returns a Range object representing a cell range specified as follows:
      • Upper-left corner cell: Range object returned by item #3 below.
      • Lower-right corner cell: Range object returned by item #4 below.
  3. Item: .Cells(FirstRow, FirstColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number FirstRow and column number FirstColumn.

      FirstRow and FirstColumn are the number of, respectively, the first row and first column in the cell range you work with. If you explicitly declare a variable to represent FirstRow or FirstColumn, use the Long data type.

  4. Item: .Cells(LastRow, LastColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number LastRow and column number LastColumn.

      LastRow and LastColumn are the number of, respectively, the last row and last column in the cell range you work with. If you explicitly declare a variable to represent LastRow or LastColumn, use the Long data type.

Line #3: .AutoFilter Field:=CriteriaField, Criteria1:=Value

  1. Item: .AutoFilter.
    • VBA Construct: Range.AutoFilter method.
    • Description: Filter the data within the range you work with using the AutoFilter and according to the parameters specified by items #2 and #3 below.
  2. Item: Field:=CriteriaField.
    • VBA Construct: Field parameter of Range.AutoFilter method.
    • Description: Specifies the field on which you want to base the filter. The leftmost field of the range you work with is Field 1. The rightmost field is the number of fields in the cell range you work with.

      If you explicitly declare a variable to represent CriteriaField, use the Long data type.

  3. Item: Criteria1:=Value.
    • VBA Construct: Criteria1 parameter of Range.AutoFilter method.
    • Description: Specifies the filtering criteria. If you explicitly declare a variable to represent Value, ensure that the data type you use can handle the value you use as criteria.

Line #4: On Error Resume Next

  1. Item: On Error Resume Next.
    • VBA Construct: On Error Resume Next statement.
    • Description: Specifies that, when a run-time error occurs, control goes to the statement following the statement where the error occurs.

      The error-handler in this line #4 is necessary because, if the field you filter by (line #3 above) doesn’t contain cells with the value you use as criteria, line #5 below generates a run-time error.

Line #5: .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count – 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete

  1. Item: Offset.
    • VBA Construct: Range.Offset property.
    • Description: Returns a Range object a number of rows above or below the cell range it works with, as returned by line #2 above.
  2. Item: RowOffset:=1.
    • VBA Construct: RowOffset parameter of Range.Offset property.
    • Description: Specifies that the cell range returned by Range.Offset (item #1 above) is 1 row below the range specified in line #2 above.

      Line #2 above specifies the cell range you work with. Therefore, the Range object that Range.Offset returns has the same size but is 1 row below the cell range you work with. This results in the following:

      • The headers of the cell range you work with are excluded from the Range object.
      • The first empty row below the last row with data (LastRow in line #2 above) is included. This extra line is handled by item #7 below.
  3. Item: Resize.
    • VBA Construct: Range.Resize property.
    • Description: Resizes the cell range returned by items #1 and #2 above.
  4. Item: RowSize.
    • VBA Construct: RowSize parameter of Range.Resize property.
    • Description: Specifies the number of rows in the new cell range returned by Range.Resize (item #3 above).
  5. Item: Rows.
    • VBA Construct: Range.Rows property.
    • Description: Returns a Range object representing the rows in the cell range it works with, as returned by line #2 above.
  6. Item: Count.
    • VBA Construct: Range.Count property.
    • Description: Returns the number of rows within the Range object returned by item #5 above.
  7. Item: Resize(RowSize:=(.Rows.Count – 1)).
    1. VBA Construct: Range.Resize property.
    2. Description: Resizes the cell range returned by items #1 and #2 above to reduce it by one row. The number of rows in the new range is obtained by subtracting 1 from the number of rows returned by line #2, as counted by items #5 and #6 above.

      This results in a cell range that excludes the first empty row below the last row with data that the Range.Offset property (items #1 and #2 above) included.

  8. Item: SpecialCells(xlCellTypeVisible).
    • VBA Construct: Range.SpecialCells method and Type parameter of Range.SpecialCells method.
    • Description: Returns a Range object representing all visible cells within the cell range you work with, excluding the headers (as required by item #2 above).

      Since line #3 above filters the data according to the criteria you specify, the visible cells returned by Range.SpecialCells are those containing the value you’re looking for in the column (field) you specify.

  9. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire rows containing the Range object returned by item #8 above.
  10. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #10 above.

Line #7: .AutoFilterMode = False

  1. Item: .AutoFilterMode = False.
    • VBA Construct: Worksheet.AutoFilterMode property.
    • Description: Specifies that the AutoFilter drop-down arrows aren’t displayed on the worksheet.

Macro Example

The following macro deletes all rows that meet the following conditions:

  • Are between:
    • Rows number (myFirstRow + 1) and myLastRow.
    • Columns number myFirstColumn and myLastColumn.
  • Contain the value myValue in field number myCriteriaField.

In this example:

  • myFirstRow is set to 5.
  • myFirstColumn is set to 2.
  • myCriteriaField is set to 1.
  • myValue is set to 5.
  • myLastRow is set to the number of the last row with data in the worksheet named “Delete row based on value”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
  • myLastColumn is set to the number of the last column with data in the same worksheet. The constructs used by the statement that finds the last column with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Column property.
Sub deleteRowBasedOnValue()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim myCriteriaField As Long
    Dim myValue As Double
    Dim myWorksheet As Worksheet

    myFirstRow = 5
    myFirstColumn = 2
    myCriteriaField = 1
    myValue = 5

    Set myWorksheet = Worksheets("Delete row based on value")

    With myWorksheet
        With .Cells
            myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            myLastColumn = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        End With
        With .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
            .AutoFilter Field:=myCriteriaField, Criteria1:=myValue
            On Error Resume Next
            .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count - 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes the rows containing myValue (5) in myCriteriaField (1).

Macro deletes rows based on value

#11: Delete Row Based on Date

VBA Code to Delete Row Based on Date

To delete rows based on the date in a specific cell using VBA, use a macro with the following statement structure:

With Worksheet
    For Counter = LastRow To FirstRow Step -1
        With .Cells(Counter, CriteriaColumn)
            If .Value = Date Then
                If Not RowsWithDate Is Nothing Then
                    Set RowsWithDate = Union(RowsWithDate, .Cells)
                Else
                    Set RowsWithDate = .Cells
                End If
            End If
        End With
    Next Counter
End With
If Not RowsWithDate Is Nothing Then RowsWithDate.EntireRow.Delete

Process Followed by VBA Code

Loop through all rows > Does row contain date? > Add cell with date to object variable representing cells with date > Return entire rows > Delete rows

VBA Statement Explanation

Lines #1 and #13: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: Statements within the With… End With statement (lines #2 through #12 below) are executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Lines #2 and #12: For Counter = LastRow To FirstRow Step -1 | Next Counter

  1. Item: For… Next Counter.
    • VBA Construct: For… Next statement.
    • Description: Repeats the statements within the loop (lines #3 through #11 below) for each row between (and including FirstRow (item #4 below) and LastRow (item #3 below).
  2. Item: Counter.
    • VBA Construct: Counter of For… Next statement.
    • Description: Loop counter. If you explicitly declare a variable to represent the loop counter, use the Long data type.
  3. Item: LastRow.
    • VBA Construct: Counter Start of For… Next statement.
    • Description: Number of the last row (further down the worksheet) you want the macro to consider when identifying blank rows. The number of the last row is also the initial value of Counter (item #2 above).

      If you explicitly declare a variable to represent the number of the last row to consider, use the Long data type.

  4. Item: FirstRow.
    • VBA Construct: Counter End of For… Next statement.
    • Description: Number of the first row (closer to the top of the worksheet) you want the macro to consider when identifying blank rows. The number of the first row is also the final value of Counter (item (#2 above).

      If you explicitly declare a variable to represent the number of the first row to consider, use the Long data type.

  5. Item: Step -1.
    • VBA Construct: Step of For… Next statement.
    • Description: Amount by which Counter (item #2 above) changes every time a loop iteration occurs.

      In this scenario, you loop backwards: from LastRow (item #3 above) to FirstRow (item #4 above). Therefore, step is -1.

Lines #3 and #11: With .Cells(Counter, CriteriaColumn) | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: Statements within the With… End With statement (lines #4 through #10 below) are executed on the cell returned by item #2 below.
  2. Item: .Cells(Counter, CriteriaColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number Counter and column number CriteriaColumn.

      At any given time, the value of the loop counter (Counter) is the same as that of the row through which the macro is currently looping. CriteriaColumn is the number of the column containing the cells with dates you consider.

Line #4: If .Value = Date Then

  1. Item: If… Then.
    • VBA Construct: Opening line of If… Then… Else statement.
    • Description: Conditionally executes the statements within the If… Then block (lines #5 through #9 below) if the condition specified by item #3 below is met.
  2. Item: .Value.
    • VBA Construct: Range.Value property.
    • Description: Returns the value of the cell represented by the Range object returned by line #3 above (.Cells(Counter, CriteriaColumn)). This is the value of the cell at the intersection of the row through which the macro is currently looping and the column containing the cells with dates you consider.
  3. Item: .Value = Date.
    • VBA Construct: Condition of If… Then… Else statement.
    • Description: This condition is a numeric expression that evaluates to True or False, as follows:
      • True: When the value of the cell at the intersection of the row through which the macro is currently looping and the column containing the cells with dates you consider is equal to the date you specify (Date).
      • False: When the value of the cell at the intersection of the row through which the macro is currently looping and the column containing the cells with dates you consider isn’t equal to the date you specify (Date).

      If you explicitly declare a variable to represent Date, ensure that the data type you use can handle the value you use as criteria. Consider, for example, using the Date data type.

      When specifying the date you use as criteria, ensure that you specify the date as a value as required by VBA. For these purposes, you can use VBA constructs such as the DateValue or DateSerial Functions.

Line #5: If Not RowsWithDate Is Nothing Then

  1. Item: If… Then.
    • VBA Construct: Opening line of If… Then… Else statement.
    • Description: Conditionally executes the statement within the If… Then… Else block (line #6 below) if the condition specified by item #6 below is met.
  2. Item: Not.
    • VBA Construct: Not operator.
    • Description: Carries out a logical negation on item #3 below. In other words, if item #3 returns:
      • True, the result is False.
      • False, the result is True.
  3. Item: RowsWithDate.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cells in the column you specify (CriteriaColumn in line #3 above) containing the date you use as criteria.
  4. Item: Is.
    • VBA Construct: Is Operator.
    • Description: Compares 2 object reference variables: (i) Not RowsWithDate (items #2 and #3 above) vs. (ii) Nothing (item #5 below).

      If both object references refer to the same object, the Is operator returns True. If they refer to different objects, Is returns False.

  5. Item: Nothing.
    • Description: The default value for a data type. In the case of an object variable (such as RowsWithDate), a null reference.
  6. Item: Not RowsWithDate Is Nothing.
    • VBA Construct: Condition of If… Then… Else statement.
    • Description: The condition is an expression that evaluates to True or False, as follows:
      • True: When “Not RowsWithDate” (items #2 and #3 above) refers to the same object as Nothing (item #5 above). This happens when RowsWithDate is “something”.

        Since RowsWithDate holds a Range object representing the cells with the criteria date found by the macro in a specific column (CriteriaColumn in line #3 above), RowsWithDate is something after the macro finds the first such cell.

      • False: When “Not RowsWithDate” refers to a different object from Nothing. This happens when RowsWithDate itself is Nothing. This occurs prior to the macro finding the first cell with the criteria date. This is because RowsWithDate isn’t assigned to anything prior to that moment.

Line #6: Set RowsWithDate = Union(RowsWithDate, .Cells)

  1. Item: Set… =.
    • VBA Construct: Set statement.
    • Description: Assigns the object reference returned by item #6 below to RowsWithDate (item #2 below).
  2. Item: RowsWithDate.
    • VBA Construct: Object (Range) variable of Set statement.
    • Description:
      • Holds a Range object representing the cells in the column you specify (CriteriaColumn in line #3 above) containing the date you use as criteria.
      • RowsWithDate is included twice in the statement. In the first mention (Set RowsWithDate), RowsWithDate is the object variable to which an object reference is assigned.
  3. Item: Union.
    • VBA Construct: Application.Union method.
    • Description: Returns a Range object representing the union of the Range objects returned by items #4 and #5 below.
  4. Item: RowsWithDate.
    • VBA Construct: Object (Range) variable.
    • Description:
      • Holds a Range object representing the cells in the column you specify (CriteriaColumn in line #3 above) containing the date you use as criteria.
      • RowsWithDate is included twice in the statement. In the second mention (Union(RowsWithDate, .Cells), RowsWithDate is one of the parameters of the Application.Union method.
  5. Item: .Cells.
    • VBA Construct: Range.Cells property.
    • Description: Returns a Range object representing the cell represented by the Range object returned by line #3 above (.Cells(Counter, CriteriaColumn)). This is the cell at the intersection of the row through which the macro is currently looping and the column containing the cells with dates you consider.
  6. Item: Union(RowsWithDate, .Cells).
    • VBA Construct: Object expression of Set statement.
    • Description: Returns the new Range object reference assigned to the RowsWithDate object variable (item #2 above). This is the union of the following 2 Range objects:
      • Prior to the Set statement, RowsWithDate represents cells in the column you specify containing the date you use as criteria found by the macro prior to the row through which it’s currently looping.
      • “.Cells” represents the cell at the intersection of the row through which the macro is currently looping and the column containing the cells with dates you consider.

      Graphically, this looks as follows:

      Union(RowsWithDate, .Cells)

      In other words, any cell containing the criteria date the macro finds is “added” to RowsWithDate.

Line #7: Else

  1. Item: Else.
    • VBA Construct: Else clause of If… Then… Else statement.
    • Description: The statement following the Else clause (line #8 below) is executed if the condition tested in the opening line of the If… Then… Else statement (line #5 above) isn’t met and returns False.

Line #8: Set RowsWithDate = .Cells

  1. Item: Set… =.
    • VBA Construct: Set statement.
    • Description: Assigns the object reference returned by item #3 below to RowsWithDate (item #2 below).
  2. Item: RowsWithDate.
    • VBA Construct: Object (Range) variable of Set statement.
    • Description: Holds a Range object representing the cells in the column you specify (CriteriaColumn in line #3 above) containing the date you use as criteria.
  3. Item: .Cells.
    • VBA Construct: Range.Cells property.
    • Description: Returns a Range object representing the cell represented by the Range object returned by line #3 above (.Cells(Counter, CriteriaColumn)). This is the cell at the intersection of the row through which the macro is currently looping and the column containing the cells with dates you consider.

Lines #9 and #10: End If | End If

  1. Item: End If.
    • VBA Construct: Closing lines of If… Then… Else statements.
    • Description: Ends the If… Then… Else statements that began in lines #4 and #5 above.

Line #14: If Not RowsWithDate Is Nothing Then RowsWithDate.EntireRow.Delete

  1. Item: If… Then.
    • VBA Construct: If… Then… Else statement.
    • Description: Conditionally executes the statement within at the end of the line (items #7 through #9 below) if the condition specified by item #6 below is met.
  2. Item: Not.
    • VBA Construct: Not operator.
    • Description: Carries out a logical negation on item #3 below. In other words, if item #3 returns:
      • True, the result is False.
      • False, the result is True.
  3. Item: RowsWithDate.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cells in the column you specify (CriteriaColumn in line #3 above) containing the date you use as criteria.
  4. Item: Is.
    • VBA Construct: Is Operator.
    • Description: Compares 2 object reference variables: (i) Not RowsWithDate (items #2 and #3 above) vs. (ii) Nothing (item #5 below).

      If both object references refer to the same object, the Is operator returns True. If they refer to different objects, Is returns False.

  5. Item: Nothing.
    • Description: The default value for a data type. In the case of an object variable (such as RowsWithDate), a null reference.
  6. Item: Not RowsWithDate Is Nothing.
    • VBA Construct: Condition of If… Then… Else statement.
    • Description: The condition is an expression that evaluates to True or False, as follows:
      • True: When “Not RowsWithDate” (items #2 and #3 above) refers to the same object as Nothing (item #5 above). This happens when RowsWithDate is “something”.

        Since RowsWithDate holds a Range object representing the cells with the criteria date found by the macro in a specific column (CriteriaColumn in line #3 above), RowsWithDate is something if the macro finds at least one such cell.

      • False: When “Not RowsWithDate” refers to a different object from Nothing. This happens when RowsWithDate itself is Nothing. This, in turn, occurs when the macro founds no cells with the criteria date within the specified column.
  7. Item: RowsWithDate.
    • VBA Construct: Object (Range) variable.
    • Description: Holds a Range object representing the cells in the column you specify (CriteriaColumn in line #3 above) containing the date you use as criteria.
  8. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire row containing the cell range returned by item #7 above.
  9. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #8 above.

Macro Example

The following macro deletes all rows that meet the following conditions:

  • Are between rows number myFirstRow and myLastRow.
  • Contain the date myDate in column number myCriteriaColumn.

In this example:

  • myFirstRow is set to 5.
  • myDate is set to the serial number representing June 15, 2017. For purposes of obtaining the appropriate serial number, I use the DateValue Function.
  • myCriteriaColumn is set to 2.
  • myLastRow is set to the number of the last row with data in the worksheet named “Delete row based on date”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
Sub deleteRowBasedOnDate()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myCriteriaColumn As Long
    Dim myDate As Date
    Dim myWorksheet As Worksheet
    Dim iCounter As Long
    Dim myRowsWithDate As Range

    myFirstRow = 6
    myCriteriaColumn = 2
    myDate = DateValue("June 15, 2017")

    Set myWorksheet = Worksheets("Delete row based on date")

    With myWorksheet
        myLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For iCounter = myLastRow To myFirstRow Step -1
            With .Cells(iCounter, myCriteriaColumn)
                If .Value = myDate Then
                    If Not myRowsWithDate Is Nothing Then
                        Set myRowsWithDate = Union(myRowsWithDate, .Cells)
                    Else
                        Set myRowsWithDate = .Cells
                    End If
                End If
            End With
        Next iCounter
    End With

    If Not myRowsWithDate Is Nothing Then myRowsWithDate.EntireRow.Delete

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes the rows containing myDate (June 15, 2017) in myCriteriaColumn (2).

Macro deletes rows based on date

#12: Delete Row Based on String Criteria

VBA Code to Delete Row Based on String Criteria

To delete rows based on the string in a specific cell using VBA, use a macro with the following statement structure:

With Worksheet
    With .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn))
        .AutoFilter Field:=CriteriaField, Criteria1:=String
        On Error Resume Next
        .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count - 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With

Process Followed by VBA Code

Identify cell range > Filter cells with string > Exclude headers > Identify visible cells > Return entire rows > Delete rows

VBA Statement Explanation

Lines #1 and #8: With Worksheet | End With

  1. Item: With… End With.
    • VBA Construct: With… End With statement.
    • Description: Statements within the With… End With statement (lines #2 through #7 below) are executed on the worksheet returned by item #2 below.
  2. Item: Worksheet.
    • VBA Construct: Workbook.Worksheets property.
    • Description: Returns a Worksheet object representing the worksheet you work with.

Lines #2 and #6: With .Range(.Cells(FirstRow, FirstColumn), .Cells(LastRow, LastColumn)) | End With

  1. Item: With… End With.
    1. VBA Construct: With… End With statement.
    2. Description: Statements within the With… End With statement (lines #3 through #6 below) are executed on the range object returned by items #2 through #4 below.
  2. Item: .Range.
    • VBA Construct: Worksheet.Range property.
    • Description: Returns a Range object representing a cell range specified as follows:
      • Upper-left corner cell: Range object returned by item #3 below.
      • Lower-right corner cell: Range object returned by item #4 below.
  3. Item: .Cells(FirstRow, FirstColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number FirstRow and column number FirstColumn.

      FirstRow and FirstColumn are the number of, respectively, the first row and first column in the cell range you work with. If you explicitly declare a variable to represent FirstRow or FirstColumn, use the Long data type.

  4. Item: .Cells(LastRow, LastColumn).
    • VBA Construct: Worksheet.Cells property and Range.Item property.
    • Description: Returns a Range object representing the cell at the intersection of row number LastRow and column number LastColumn.

      LastRow and LastColumn are the number of, respectively, the last row and last column in the cell range you work with. If you explicitly declare a variable to represent LastRow or LastColumn, use the Long data type.

Line #3: .AutoFilter Field:=CriteriaField, Criteria1:=String

  1. Item: .AutoFilter.
    • VBA Construct: Range.AutoFilter method.
    • Description: Filter the data within the range you work with using the AutoFilter and according to the parameters specified by items #2 and #3 below.
  2. Item: Field:=CriteriaField.
    • VBA Construct: Field parameter of Range.AutoFilter method.
    • Description: Specifies the field on which you want to base the filter. The leftmost field of the range you work with is Field 1. The rightmost field is the number of fields in the cell range you work with.

      If you explicitly declare a variable to represent CriteriaField, use the Long data type.

  3. Item: Criteria1:=String.
    • VBA Construct: Criteria1 parameter of Range.AutoFilter method.
    • Description: Specifies the filtering criteria. If you explicitly declare a variable to represent String, use the String data type.

Line #4: On Error Resume Next

  1. Item: On Error Resume Next.
    • VBA Construct: On Error Resume Next statement.
    • Description: Specifies that, when a run-time error occurs, control goes to the statement following the statement where the error occurs.

      The error-handler in this line #4 is necessary because, if the field you filter by (line #3 above) doesn’t contain cells with the string you use as criteria, line #5 below generates a run-time error.

Line #5: .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count – 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete

  1. Item: Offset.
    • VBA Construct: Range.Offset property.
    • Description: Returns a Range object a number of rows above or below the cell range it works with, as returned by line #2 above.
  2. Item: RowOffset:=1.
    • VBA Construct: RowOffset parameter of Range.Offset property.
    • Description: Specifies that the cell range returned by Range.Offset (item #1 above) is 1 row below the range specified in line #2 above.

      Line #2 above specifies the cell range you work with. Therefore, the Range object that Range.Offset returns has the same size but is 1 row below the cell range you work with. This results in the following:

      • The headers of the cell range you work with are excluded from the Range object.
      • The first empty row below the last row with data (LastRow in line #2 above) is included. This extra line is handled by item #7 below.
  3. Item: Resize.
    • VBA Construct: Range.Resize property.
    • Description: Resizes the cell range returned by items #1 and #2 above.
  4. Item: RowSize.
    • VBA Construct: RowSize parameter of Range.Resize property.
    • Description: Specifies the number of rows in the new cell range returned by Range.Resize (item #3 above).
  5. Item: Rows.
    • VBA Construct: Range.Rows property.
    • Description: Returns a Range object representing the rows in the cell range it works with, as returned by line #2 above.
  6. Item: Count.
    • VBA Construct: Range.Count property.
    • Description: Returns the number of rows within the Range object returned by item #5 above.
  7. Item: Resize(RowSize:=(.Rows.Count – 1)).
    1. VBA Construct: Range.Resize property.
    2. Description: Resizes the cell range returned by items #1 and #2 above to reduce it by one row. The number of rows in the new range is obtained by subtracting 1 from the number of rows returned by line #2, as counted by items #5 and #6 above.

      This results in a cell range that excludes the first empty row below the last row with data that the Range.Offset property (items #1 and #2 above) included.

  8. Item: SpecialCells(xlCellTypeVisible).
    • VBA Construct: Range.SpecialCells method and Type parameter of Range.SpecialCells method.
    • Description: Returns a Range object representing all visible cells within the cell range you work with, excluding the headers (as required by item #2 above).

      Since line #3 above filters the data according to the criteria you specify, the visible cells returned by Range.SpecialCells are those containing the string you’re looking for in the column (field) you specify.

  9. Item: EntireRow.
    • VBA Construct: Range.EntireRow property.
    • Description: Returns a Range object representing the entire rows containing the Range object returned by item #8 above.
  10. Item: Delete.
    • VBA Construct: Range.Delete method.
    • Description: Deletes the Range object returned by item #10 above.

Line #7: .AutoFilterMode = False

  1. Item: .AutoFilterMode = False.
    • VBA Construct: Worksheet.AutoFilterMode property.
    • Description: Specifies that the AutoFilter drop-down arrows aren’t displayed on the worksheet.

Macro Example

The following macro deletes all rows that meet the following conditions:

  • Are between:
    • Rows number (myFirstRow + 1) and myLastRow.
    • Columns number myFirstColumn and myLastColumn.
  • Contain the string myString in field number myCriteriaField.

In this example:

  • myFirstRow is set to 5.
  • myFirstColumn is set to 2.
  • myCriteriaField is set to 1.
  • myString is set to “*to delete*”.

    The asterisks at the beginning and end of the string act as wildcards representing any number of characters. Therefore, myString includes any strings that contain “to delete”, regardless of the text before or after it.

    For example, in the example below, I use this macro to delete rows where the cell in the first column contains the string “Rows to delete now”. “to delete” is between the strings “Rows ” and ” now”, both of which are covered by the asterisk wildcard.

  • myLastRow is set to the number of the last row with data in the worksheet named “Delete row based on string”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.
  • myLastColumn is set to the number of the last column with data in the same worksheet. The constructs used by the statement that finds the last column with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Column property.
Sub deleteRowBasedOnString()

    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-delete-row/

    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim myCriteriaField As Long
    Dim myString As String
    Dim myWorksheet As Worksheet

    myFirstRow = 5
    myFirstColumn = 2
    myCriteriaField = 1
    myString = "*to delete*"

    Set myWorksheet = Worksheets("Delete row based on string")

    With myWorksheet
        With .Cells
            myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            myLastColumn = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        End With
        With .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
            .AutoFilter Field:=myCriteriaField, Criteria1:=myString
            On Error Resume Next
            .Offset(RowOffset:=1).Resize(RowSize:=(.Rows.Count - 1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With

End Sub

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA deletes the rows containing myString (“*to delete*”) in myCriteriaField (1).

Macro deletes rows based on string criteria

References to VBA Constructs Used in this VBA Tutorial

Use the following links to visit the appropriate webpage within the Microsoft Office Dev Center:

  1. Identify the worksheet you work with:
    • Workbook.Worksheets property.
  2. Find last row and last column with data in a worksheet and count number of rows in a cell range:
    • Range.Find method.
    • Range.Count property.
  3. Return Range objects:
    • Application.ActiveCell property.
    • Worksheet.Cells property.
    • Range.Cells property.
    • Range.Offset property.
    • Range.Resize property.
    • Application.Union method.
  4. Return Range objects representing rows:
    • Worksheet.Rows property.
    • Range.Rows property.
    • Range.EntireRow property.
  5. Loop through rows:
    • For… Next statement.
  6. Specify criteria for row deletion:
    • DateSerial Function.
    • DateValue Function.
    • Range.Value property.
  7. Test if (i) rows meet criteria for deletion, or (ii) the macro has found rows or cells meeting the criteria for deletion:
    • If… Then… Else statement.
    • Range.AutoFilter method.
    • Range.SpecialCells method.
    • WorksheetFunction.CountA method.
    • Not operator.
    • Is operator.
  8. Delete rows.
    • Range.Delete method.
  9. Work with variables:
    • Dim statement.
    • Set statement.
    • Data types:
      • Data data type.
      • Double data type.
      • Long data type.
      • Object data type.
      • String data type.
      • Variant data type.
  10. Simplify object references:
    • With… End With statement.
  11. Handle errors:
    • On Error statement.
  12. Remove AutoFilter drop-down arrows:
    • Worksheet.AutoFilterMode property.

Skip to content

VBA Delete Row – Excel Examples Macros Codes

Home » VBA » VBA Delete Row – Excel Examples Macros Codes

  • Excel VBA Code Example Macro to Delete Row

Excel VBA code to Delete Rows examples Macros will help us to delete rows from excel worksheet in various criteria. We can use VBA delete row macros to delete entire row, blank cells or empty rows, move up or shift up, delete row if cell contains a string, date or value, from a sheet, table and range. We will use Rows().Delete method to delete the rows in of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013. Excel VBA Macro code for deleting rows macro will help you to know how to delete rows in excel.

Excel VBA Code Example Macro to Delete Row

VBA code to Delete Rows in Excel

Following are the VBA Syntax and sample VBA macro commands to Delete Rows from worksheet using Excel VBA. We will see the various examples to delete the rows in different situations.

VBA code Example Macros to Delete Entire Row

The following Excel VBA Example macro code is to delete entire row from the worksheet. This VBA macro will delete the first row in the worksheet.

Sub sbVBS_To_Delete_EntireRow ()
    Rows(1).EntireRow.Delete
End Sub

If you execute this macro, this will delete the first row from the worksheet as we mentioned 1 in the code. Similarly, we can use any number to delete the specific row using VBA.

Rows(

[Row Number]).EntireRow.Delete

Here Row Number is your row number to delete. And EntireRow.Delete method will delete the Entire row from the Excel spreadsheet.

Delete Entire Row using VBA – Excel Macro Example Code

VBA Delete Row:Example Macro code to Delete Active Row

The following Excel VBA Example macro code is to delete active row from the worksheet. This VBA macro will delete the row of the currently active cell in the worksheet.

Sub sbVBS_To_Delete_Active_Rows()
Rows(ActiveCell.Row).Delete
End Sub 
 

If you execute this macro, this will delete the active row from the worksheet as we mentioned ActiveCell.Row in the code. ActiveCell.Row returns the row number of the currently active cell.

Rows(ActiveCell.Row).EntireRow.Delete

Here ActiveCell.Row return the row number of the active cell. And EntireRow.Delete method will delete the rows from the Excel spreadsheet.
Delete Active row using VBA

VBA code Example Macros to delete blank rows in a Range

The following Excel VBA Example macro code is to delete blank rows in a range from the worksheet. This VBA macro will delete the blank rows in a range in the worksheet.

Sub sbVBS_To_Delete_Blank_Rows_In_Range()
Dim iCntr
Dim rng As Range
Set rng = Range("A10:D20")

    For iCntr = rng.Row + rng.Rows.Count - 1 To rng.Row Step -1
        If Application.WorksheetFunction.CountA(Rows(iCntr)) = 0 Then Rows(iCntr).EntireRow.Delete
    Next

End Sub 

If you execute this macro, this will delete the blank rows in a range. We are checking the blank rows using if condition and CountA function to determine the row is blank. And then we are using Delete method of rows to delete the rows in a Excel range.

If Application.WorksheetFunction.CountA(Rows(RowNumber)) = 0 Then Rows(RowNumber).EntireRow.Delete

Here Application.WorksheetFunction.CountA will check if the row is blank. And the Rows.delete method will delete the row if row is blank.
Delete blank rows in a range using VBA

VBA code Example Macros to delete blank rows in a Table

The following Excel VBA Example macro code is to delete blank rows in a table from the worksheet. This VBA macro will delete the blank rows in a table in the worksheet.

Sub sbVBS_To_Delete_Blank_Rows_In_Table()
Dim iCntr As Long
Dim rng As Range

Set rng = ActiveSheet.ListObjects("Table1").Range

For iCntr = rng.Row + rng.Rows.Count - 1 To rng.Row Step -1
        If Application.WorksheetFunction.CountA(Rows(iCntr)) = 0 Then Rows(iCntr).EntireRow.Delete
    Next
End Sub 

If you execute this macro, this will delete the blank rows in table. We are checking the blank rows using if condition and CountA function to determine the row is blank in the table. And then we are using Delete method of rows to delete the rows in a table.


If Application.WorksheetFunction.CountA(Rows(RowNumber)) = 0 Then Rows(RowNumber).EntireRow.Delete

Here Application.WorksheetFunction.CountA will check if the row is blank. And the Rows.delete method will delete the row if row is blank.
Delete blank rows in a table using VBA

VBA code Example Macros to delete hidden rows

The following Excel VBA Example macro code is to hidden rows from the worksheet. This VBA macro will delete the hidden rows in a worksheet.

Sub sbVBS_To_Delete_Hidden_Rows()
Dim lastRow
lastRow = 100
    For iCntr = lastRow To 1 Step -1
        If Rows(iCntr).Hidden = True Then Rows(iCntr).EntireRow.Delete
    Next
End Sub 

If you execute this macro, this will delete the hidden rows in a worksheet. We are checking the rows using Hidden property of a row and then deleting if the row is hidden.

If Rows(RowNumber).Hidden = True Then Rows(RowNumber).EntireRow.Delete

Here Rows(RowNumber).Hidden is to check if the row is hidden. And EntireRow.Delete method will delete the rows from the Excel spreadsheet if they are hidden.
Delete Hidden Rows using VBA

VBA code Example Macros to delete multiple rows

The following Excel VBA Example macro code is to multiple rows from the worksheet. This VBA macro will delete the multiple rows at one shot from a worksheet.

Sub sbVBS_To_Delete_Multiple_Rows ()
    Rows(“1:3”).EntireRow.Delete
End Sub

If you execute this macro, this will delete the multiple rows from a worksheet. We can specify any number of rows (starting and ending row number) based on the system memory.


Rows(“[Row Numbers]”).EntireRow.Delete

Here Row Numbers are your row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete Multiple Rows using VBA – Excel Macro Example Code

VBA code Example Macros to delete rows based on Cell Color

Following is the VBA syntax and sample VBA code to delete rows based on Cell fill Color from worksheet using VBA. We are using the Delete method of the Rows object of worksheet to delete rows.

Sub sbDelete_Rows_Based_On_Cell_Color()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1).Interior.ColorIndex = 3 Then ‘3=Red
        Rows(iCntr).Delete
    End If
Next
End Sub
The above VBA code is to delete rows based on Cell Color from the excel worksheet. This code will delete the rows (1 to 20) if it satisfy the color condition if cell background is red (Interior.ColorIndex=3).

<code>
If <color criteria> Then Rows(“[Row Numbers]”).EntireRow.Delete 
</code>

Here <color criteria> is the color condition which you want to check to delete rows. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
<a href="https://analysistabs.com/vba/delete-rows-based-on-cell-color-excel-macro-example-code/" class="more-link" title="Delete rows based on Cell Color using VBA">Delete rows based on Cell Color using VBA</a>

<H3>VBA code Example Macros to delete rows based on Cell font Color </H3>
Following is the VBA syntax and sample VBA code to delete rows based on Cell font Color from worksheet using VBA.  We are using the Delete method of the Rows object of worksheet to delete rows.
 
Sub sbDelete_Rows_Based_On_Cell_Color()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1).Font.ColorIndex = 3 Then ‘3=Red
        Rows(iCntr).Delete
    End If
Next
End Sub

The above VBA code is to delete rows based on Cell font Color from the excel worksheet. This code will delete the rows (1 to 20) if it satisfy the color condition if cell font color is red (Font.ColorIndex = 3).


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here is the color condition which you want to check to delete rows. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows based on Cell font Color using VBA

VBA code Example Macros to delete rows based on cell value

Following is the VBA syntax and sample VBA code to delete rows based on cell value from worksheet using VBA. We are checking the cell value and using the Delete method of the Rows object of worksheet to delete rows.

Sub sbDelete_Rows_Based_On_Criteria()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1) = 10 Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here cell value criteria is the condition which you want to check the cells to delete rows. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows based on cell value using Excel VBA

VBA code Example Macros to delete rows based on Criteria

Following is the VBA syntax and sample VBA code to delete rows based on criteria from worksheet using VBA. We are checking the cell for criteria and using the Delete method of the Rows object of worksheet to delete rows.

Sub sbDelete_Rows_Based_On_Criteria()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1) = 1 Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here criteria is the condition which you want to check to delete rows. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows based on criteria using VBA

VBA code Example Macros to delete rows based on Date

Following is the VBA syntax and sample VBA code to delete rows based on Date from worksheet using VBA. We are checking the cell value for a specific date and using the Delete method of the Rows object of worksheet to delete rows.

Sub sbDelete_Rows_Based_On_Date()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Format(Cells(iCntr, 1), "dd-mm-yyyy”) = Format(Now(), "dd-mm-yyyy") Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here is the date condition which you want to check to delete rows. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows based on Date using VBA

VBA code Example Macros to delete rows based on multiple criteria

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets based o multiple criteria. This will help you to know how to delete specific rows based on multiple conditions from Excel workbook using VBA.

Sub sbDelete_Rows_Based_On_Multiple_Criteria()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1) = 1 or trim(Cells(iCntr, 1)) = “” Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here is the conditions which you want to check to delete rows. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows based on multiple criteria using VBA

VBA code Example Macros to delete rows if cell contains Error value

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets if cell contains Error value. This will help you to know how to delete specific rows if cell contains Error value from Excel workbook using VBA.

Sub sbDelete_Rows_IF_Cell_Contains_Error()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If IsError(Cells(iCntr, 1)) Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here to check if the cell contains any error. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows if cell contains Error value using VBA

VBA code Example Macros to delete rows if cell contains Number value

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets if cell contains Number value. This will help you to know how to delete specific rows if cell contains Number value from Excel workbook using VBA.

Sub sbDelete_Rows_IF_Cell_Contains_Error()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If isNumeric(Cells(iCntr, 1)) Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here to check if the cell contains any error. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows if cell contains Number value using VBA

VBA code Example Macros to delete rows if cell contains string value

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets if cell contains string value. This will help you to know how to delete specific rows if cell contains string value from Excel workbook using VBA.

Sub sbDelete_Rows_IF_Cell_Cntains_String_Text_Value()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1) = “Your String” Then ‘ You can change this text
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here to check if the cell contains a specific text. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows if cell contains string value using VBA

VBA code Example Macros to delete rows if cell is 0 (equals to zero)

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets if cell is 0 (equals to zero). This will help you to know how to delete specific rows if cell is 0 (equals to zero) from Excel workbook using VBA.

Sub sbDelete_Rows_IF_Cell_Contains_Error()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1)=0 Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If < (cell value)=0> Then Rows(“[Row Numbers]”).EntireRow.Delete

Here < (cell value)=0> to check if the cell contains 0 (zero). And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete rows if cell is 0 (equals to zero) using VBA

VBA code Example Macros to delete rows if Cell is Empty

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets if Cell is Empty/Blanks. This will help you to know how to delete specific rows if Cell is Empty/Blanks from Excel workbook using VBA.

Sub sbDelete_Rows_IF_Cell_Is_Blank()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If trim(Cells(iCntr, 1)) = “” Then
        Rows(iCntr).Delete
    End If
Next
End Sub


If trim() =”” Then Rows(“[Row Numbers]”).EntireRow.Delete

Here trim() =””is to check if the cell is empty. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.
Delete row if Cell is Empty/Blank using Excel VBA

VBA code Example Macros to delete rows in range

Here is the Example VBA syntax and Example VBA Macro to delete rows in range from excel worksheets. This will help you to know how to delete specific rows in a range from Excel workbook using VBA.

Sub sbVBS_To_Delete_Rows_In_Range()
Dim iCntr
Dim rng As Range
Set rng = Range("A10:D20")

    For iCntr = rng.Row + rng.Rows.Count - 1 To rng.Row Step -1
       Rows(iCntr).EntireRow.Delete
    Next

End Sub 


Rows(RowNumber).EntireRow.Delete

Here Rows.delete method will delete the row in the worksheet.
Delete rows in a range using VBA

VBA code Example Macros to delete rows shift up

Here is the Example VBA syntax and Example VBA Macro to delete rows to shift up cells from excel worksheets. This will help you to know how to delete specific rows shift up from Excel workbook using VBA.

Sub sbDelete_Rows_IF_Cell_Contains_Error()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1)=0 Then
        Range("A" & iCntr).Delete Shift:=xlUp    End If
Next
End Sub


Range("Your Range”).Delete Shift:=xlUp

Here Range("Your Range”) is your range to delete. And Shift:=xlUp tells excel to shift up the cells not to delete entire row.
Delete rows shift up using VBA

VBA code Example Macros to delete rows with specific data values

Here is the Example VBA syntax and Example VBA Macro to delete rows from excel worksheets with specific data values. This will help you to know how to delete specific rows with specific data values from Excel workbook using VBA.

Sub sbDelete_Rows_With_Specific_Data ()
Dim lRow As Long
Dim iCntr As Long
lRow = 20
For iCntr = lRow To 1 Step -1
    If Cells(iCntr, 1) = “Your Data” Then ‘ You can change this text
  ‘If Cells(iCntr, 1) = “22-12-2013” Then ‘ to check specific date
        Rows(iCntr).Delete
    End If
Next
End Sub


If Then Rows(“[Row Numbers]”).EntireRow.Delete

Here to check if the cell contains a specific data. And Row Numbers are the row numbers to delete. And EntireRow.Delete method will delete the Entire rows from the Excel spreadsheet.

Delete rows with specific value using VBA – Excel VBA Macro Example Code

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Excel VBA Project Management Templates
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project Management Template

Ultimate Resource Management Template

Project Portfolio Management Templates

Related Posts

  • VBA code to Delete Rows in Excel
    • VBA code Example Macros to Delete Entire Row
    • VBA Delete Row:Example Macro code to Delete Active Row
    • VBA code Example Macros to delete blank rows in a Range
    • VBA code Example Macros to delete blank rows in a Table
    • VBA code Example Macros to delete hidden rows
    • VBA code Example Macros to delete multiple rows
    • VBA code Example Macros to delete rows based on Cell Color
    • VBA code Example Macros to delete rows based on cell value
    • VBA code Example Macros to delete rows based on Criteria
    • VBA code Example Macros to delete rows based on Date
    • VBA code Example Macros to delete rows based on multiple criteria
    • VBA code Example Macros to delete rows if cell contains Error value
    • VBA code Example Macros to delete rows if cell contains Number value
    • VBA code Example Macros to delete rows if cell contains string value
    • VBA code Example Macros to delete rows if cell is 0 (equals to zero)
    • VBA code Example Macros to delete rows if Cell is Empty
    • VBA code Example Macros to delete rows in range
    • VBA code Example Macros to delete rows shift up
    • VBA code Example Macros to delete rows with specific data values

VBA Reference

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:

Effectively Manage Your
Projects and  Resources

With Our Professional and Premium Project Management Templates!

ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.

We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.

Project Management
Excel VBA

Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.

Analysistabs Logo

Page load link

Go to Top

Like this post? Please share to your friends:
  • Excel delete all values
  • Excel delete all numbers
  • Excel delete all but one sheet
  • Excel delete all but one duplicate
  • Excel delete all blank rows