大量資料中夾雜空白列,不只佔版面、還干擾篩選、樞紐分析、圖表繪製。Excel 內建「定位條件 → 空值」功能,配合「刪除列」,幾秒鐘就能批量清除所有空白列,適用 Excel 2007 至 Office 365,含巨集自動化版本。
標準法會把「只要有一格空白」的列整列刪除(因為該空白儲存格被選中)。解法:
=COUNTBLANK(A2:X2)(A2:X2 為該列實際資料欄位範圍) → 向下複製填滿。使用 VBA 巨集直接在記憶體陣列處理,速度快 100 倍:
Sub DeleteBlankRowsFast()
Dim rng As Range, arr As Variant, i As Long, j As Long, keepRows As Collection
Set keepRows = New Collection
Set rng = ActiveSheet.UsedRange
arr = rng.Value
For i = 1 To UBound(arr, 1)
Dim isBlank As Boolean: isBlank = True
For j = 1 To UBound(arr, 2)
If arr(i, j) <> "" Then isBlank = False: Exit For
Next j
If Not isBlank Then keepRows.Add i
Next i
Application.ScreenUpdating = False
' 從下往上刪除不在 keepRows 中的列
Dim lastRow As Long: lastRow = rng.Rows.Count
Dim keepDict As Object: Set keepDict = CreateObject("Scripting.Dictionary")
For Each i In keepRows: keepDict(i) = 1: Next
For i = lastRow To 1 Step -1
If Not keepDict.exists(i) Then rng.Rows(i).EntireRow.Delete
Next i
Application.ScreenUpdating = True
MsgBox "完成!保留 " & keepRows.Count & " 列資料。"
End Sub
掌握「選區 → F5 → 空值 → 右鍵刪除整列」標準流程,再視資料特性套用輔助欄/篩選/VBA 進階法,Excel 清理空白列從此不再是耗時苦工,而是幾秒鐘的標準動作。