Using Excel VBA to manipulate entire rows lets you automate tasks like clearing data, deleting rows, or applying formatting across all columns in a row—saving hours on repetitive operations. This works in all modern versions of Excel (2010+) on Windows and macOS, and requires no add-ins, only basic VBA access enabled in Excel’s Trust Center.

What You'll Need

  • Microsoft Excel 2010 or later (Windows or macOS)
  • Developer tab enabled in Excel (Settings > Customize Ribbon > check Developer)
  • A workbook with data you can safely modify (always backup first)
  • Basic familiarity with VBA Editor (press Alt+F11 to open)

Step-by-Step

  1. Open your Excel workbook and press Alt+F11 to launch the Visual Basic for Applications editor.
  2. In the Project Explorer (left panel), right-click your workbook name and select Insert > Module. This creates a blank code window.
  3. Copy and paste one of the following VBA code blocks into the module, depending on your goal:
  4. To select an entire row (e.g., row 5): Rows(5).Select
  5. To clear all content from row 5 (keeps formatting): Rows(5).ClearContents
  6. To delete row 5 entirely (shifts rows up): Rows(5).Delete
  7. To format row 5 with bold text and yellow background: With Rows(5) .Font.Bold = True .Interior.Color = RGB(255, 255, 0) End With
  8. If you want to target the active row (where your cursor is), use ActiveCell.EntireRow instead of Rows(n)—for example: ActiveCell.EntireRow.Delete
  9. To loop through multiple rows (e.g., rows 10 to 20), use a For loop: Dim i As Integer For i = 10 To 20 Rows(i).ClearContents Next i
  10. To target rows based on a condition (e.g., delete rows where column A is blank): Dim i As Long For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1 If IsEmpty(Cells(i, 1)) Then Rows(i).Delete Next i
  11. Save the workbook as a macro-enabled file (.xlsm) when prompted. Excel will warn you—click Yes.
  12. Press F5 while in the VBA editor to run the code, or return to Excel and press Alt+F8 to open the Macro dialog, select your macro, and click Run.

Common Mistakes

  • Using Rows.Delete without specifying a row number—this causes a runtime error because Excel doesn’t know which row.
  • Deleting rows in ascending order (1 to 100) when using a loop: this causes skipped rows because deleting shifts data up, so row 2 becomes row 1, but the loop moves to row 2 anyway. Always loop backwards (from high to low) when deleting.
  • Forgetting to save as .xlsm, then losing the macro when reopening the file.
  • Using Cells(row, col) without defining the worksheet—this references the active sheet, which may not be the one you intended. Always specify: Worksheets("Sheet1").Rows(5).Delete
  • Assuming ClearContents removes formatting—it doesn’t. Use Clear if you want to remove both data and formatting.

Tips

  • Use Application.ScreenUpdating = False at the start and True at the end of macros that process many rows—it speeds up execution dramatically.
  • Always declare variables: use Dim i As Long instead of just i. This prevents type errors and improves performance.
  • Test macros on a copy of your data first. Use a duplicate worksheet or a separate test workbook.
  • To find the last row with data: LastRow = Cells(Rows.Count, "A").End(xlUp).Row. This is safer than hardcoding row numbers.
  • Use On Error Resume Next only if you know the risk—otherwise, let errors show so you can fix them.
  • Comment your code with apostrophes (') to explain what each block does. Future you will thank you.

Troubleshooting

  • "Runtime error 9: Subscript out of range"—This means you referenced a sheet name that doesn’t exist. Check spelling in Worksheets("SheetName"). Use ActiveWorkbook.Worksheets.Count to list all sheet names in the Immediate Window (Ctrl+G in VBA).
  • Macro doesn’t run or disappears—Ensure the file is saved as .xlsm. If you saved as .xlsx, macros are stripped. Reopen and resave correctly.
  • Code runs but does nothing—Check if you’re targeting the right worksheet. Add Debug.Print ActiveSheet.Name to print the active sheet name in the Immediate Window.
  • Excel freezes during row deletion—You’re likely looping through thousands of rows without Application.ScreenUpdating = False. Add it and try again. Also, avoid deleting more than 10,000 rows at once—break it into chunks.
  • "Object required" error—You used an object method on a non-object. For example, Rows.Delete without a qualifying object. Always use Rows(n).Delete or Range("A5").EntireRow.Delete.

Frequently Asked Questions

How do I delete rows where a cell in column B contains "No"?

Use this code: Dim i As Long For i = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1 If Cells(i, 2).Value = "No" Then Rows(i).Delete Next i

Can I format an entire row based on a value in another column?

Yes. Use conditional logic: If Cells(i, 3).Value > 100 Then Rows(i).Interior.Color = RGB(144, 238, 144)—this colors the row green if column C is over 100.

Why does my code delete the wrong row?

You likely used ActiveCell.Row but the active cell changed during execution. Always define the row explicitly: Rows(10), not ActiveCell.EntireRow in loops.

Can I use entire row operations on filtered data?

Yes, but Rows.Delete will delete all rows in the range, even hidden ones. To delete only visible rows: use Range("A1:A100").SpecialCells(xlCellTypeVisible).EntireRow.Delete after applying a filter.

What’s the difference between ClearContents and Clear?

ClearContents removes only data. Clear removes data, formatting, comments, and hyperlinks. Use Clear if you want a completely blank row.

Why does Excel crash when I run my row deletion macro?

You’re likely deleting too many rows at once, or referencing a non-existent sheet. Break the operation into smaller loops (e.g., 500 rows at a time) and add DoEvents between batches to let Excel respond. Always backup before running large deletions.