You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

工作表含两个表格无法删行,如何将A-E列空行上移?

解决多表格工作表中A-E列空白行向上移位的问题

原代码最后一行的Range("A:E").SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp会删除空白单元格所在的整行,这会破坏工作表里的其他表格。下面是修改后的代码,仅针对A-E列全空的行,在A-E列范围内将内容向上移位,不会影响其他列的内容:

Sub MoveBlankRowsUpInAE()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim r As Long
    Dim isRowBlank As Boolean
    Dim col As Integer
    
    Set ws = ThisWorkbook.Sheets("Current")
    lastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row ' 沿用原逻辑,按B列定位最后一行
    
    ' 执行原有的归档操作(将"Done"行移到Archive表)
    With Sheets("Archive")
        For r = 1 To lastRow
            If ws.Cells(r, 4) = "Done" Then
                lr = .Cells(Rows.Count, 2).End(xlUp).Row + 1
                For Each e In Array("A", "B", "C", "D", "E")
                    .Range(e & lr) = ws.Range(e & r)
                    ws.Range(e & r).ClearContents
                Next e
            End If
        Next r
    End With
    
    ' 从下往上遍历,处理A-E列的全空白行
    For r = lastRow To 1 Step -1
        isRowBlank = True
        ' 检查当前行A到E列是否全部为空
        For col = 1 To 5
            If Not IsEmpty(ws.Cells(r, col)) Then
                isRowBlank = False
                Exit For
            End If
        Next col
        
        If isRowBlank Then
            ' 仅在A-E列范围内,将下方内容向上移动一行
            ws.Range("A" & r & ":E" & lastRow).Delete Shift:=xlUp
            lastRow = lastRow - 1 ' 内容上移后,最后一行索引减1
        End If
    Next r
End Sub

关键修改说明

  • 从下往上遍历:避免因内容上移导致行索引变化,漏处理后续行。
  • 精准判断全空行:循环检查A-E列的每个单元格,只有整列范围全空才触发操作。
  • 仅操作A-E列:删除和上移动作限制在A-E列范围内,完全不影响工作表中其他表格的内容。

内容的提问来源于stack exchange,提问作者Raymond

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:03:16