{"id":36,"date":"2018-06-27T11:24:12","date_gmt":"2018-06-27T11:24:12","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=36"},"modified":"2024-03-29T15:48:43","modified_gmt":"2024-03-29T15:48:43","slug":"deleting-a-row-with-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/deleting-a-row-with-vba\/","title":{"rendered":"Deleting a Row With VBA"},"content":{"rendered":"\n

In Excel, you can delete a row by right-clicking on the number of the row and then selecting Delete. But when you want to delete rows using more sophisticated methods you have to use VBA. It will allow you to delete rows in many different ways.<\/p>\n\n\n\n

Delete a specific row<\/h2>\n\n\n\n

The most basic way to delete a row in VBA<\/a> is by using the row number.<\/p>\n\n\n\n

Sub deleteSpecificRow()\nRows(4).Delete\nEnd Sub<\/pre>\n\n\n\n

This will delete the fourth row each time you execute the macro.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Delete multiple rows<\/h2>\n\n\n\n

You can also delete a few rows at once. Instead of selecting the row number, select a range<\/a> of rows.<\/p>\n\n\n\n

Sub deleteMultipleRows()\nRows(\"2:4\").Delete\nEnd Sub<\/pre>\n\n\n\n

It will delete rows 2, 3, and 4.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Delete selected rows<\/h2>\n\n\n\n

So far we created macros that deleted rows with fixed numbers<\/a> inside the code. This time, let\u2019s delete rows from a selection.  Let\u2019s use the following code.<\/p>\n\n\n\n

Sub deleteSelectedRows()\nSelection.EntireRow.Delete\nEnd Sub<\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

Delete blank rows<\/h2>\n\n\n\n

In this example, Excel identifies which cells are blank, selects them, and removes the rows in which they are present.<\/p>\n\n\n\n

Sub deleteBlankRows()\nSelection.SpecialCells(xlCellTypeBlanks).Select\nSelection.EntireRow.Delete\nEnd Sub<\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

This macro deletes rows from selection only if it contains the phrase \u201cBad Row\u201d.<\/p>\n\n\n\n

CAUTION<\/h3>\n\n\n\n

Remember that text is case-sensitive.<\/p>\n\n\n\n

Sub deleteRowswithSelectedText()\nFor Each Cell In Selection\n   If Cell.Value = \"Bad Row\" Then\n      Rows(Cell.Row).ClearContents\n   End If\nNext\nSelection.SpecialCells(xlCellTypeBlanks).Select\nSelection.EntireRow.Delete\nEnd Sub<\/pre>\n\n\n\n

Code analysis<\/strong>:<\/p>\n\n\n\n

2.<\/strong> The loop goes through each element in the selection.<\/p>\n\n\n\n

3<\/strong>. Excel checks the actual cell value<\/a> and if the value is exactly \u201cBad Row\u201d.<\/p>\n\n\n\n

4.<\/strong> If the condition is met, Excel clears the contents of the cell. We can\u2019t delete the cell because the cell position will change and some of the \u201cBad Rows\u201d may stay.<\/p>\n\n\n\n

7.<\/strong> When all \u201cBad Rows\u201d are cleared then Excel selects these cells.<\/p>\n\n\n\n

8.<\/strong> Then deletes rows of these cells.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

If you also want to delete those rows that consist of this phrase, and not only the exact match, use the InStr function. This function returns the position of the first occurrence of the phrase.<\/p>\n\n\n\n

change<\/p>\n\n\n\n

Cell.Value = \"Bad Row\"<\/pre>\n\n\n\n

to<\/p>\n\n\n\n

InStr(Cell, \"Bad Row\") > 0<\/pre>\n\n\n\n

We have four different examples inside our worksheet. Each of them returns a position.<\/p>\n\n\n\n