Option Explicit ' OneXcel Studio | Delete blank rows (safe version) ' Deletes rows that are completely empty across the whole sheet. ' Checks the selected rows, or the used range when only one cell is selected. ' A row with anything in it (a space, a note in a far column) is kept. Public Sub DeleteBlankRows() Dim ws As Worksheet, rng As Range Dim r As Long, firstRow As Long, lastRow As Long, n As Long Dim answer As VbMsgBoxResult Set ws = ActiveSheet If ws.ProtectContents Then MsgBox "Unprotect the sheet first (Review > Unprotect Sheet).", vbExclamation Exit Sub End If ' 1. Rows to check: the selection, or the used range of the sheet If TypeName(Selection) = "Range" And Selection.Cells.Count > 1 Then Set rng = Selection.Areas(1) Else Set rng = ws.UsedRange End If firstRow = rng.Row lastRow = rng.Row + rng.Rows.Count - 1 ' 2. Count the rows that are empty across the whole sheet width For r = firstRow To lastRow If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then n = n + 1 Next r If n = 0 Then MsgBox "No blank rows found in rows " & firstRow & " to " & lastRow & ".", vbInformation Exit Sub End If ' 3. Ask once, then delete from the bottom up (so the rows still to check do not move) answer = MsgBox("Delete " & n & " blank rows in rows " & firstRow & " to " & lastRow & "?", _ vbYesNo + vbQuestion, "Delete blank rows") If answer <> vbYes Then Exit Sub Application.ScreenUpdating = False For r = lastRow To firstRow Step -1 If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then ws.Rows(r).Delete Next r Application.ScreenUpdating = True MsgBox n & " blank rows deleted.", vbInformation End Sub