使用VBA删除Excel中的空行

时间:2019-11-26 22:34:39

标签: excel vba

我正在尝试通过使用以下代码在excel中删除空行:

iRowLast = .Cells(.Rows.Count, iCol_Exp).End(xlUp).Row
Set r = .Range("A1:F48")
Set Z = ActiveSheet.Range("A1:Z50")

For i = iRowLast To 10 Step (-1)
    r.Rows(i).Replace What:=Chr(32), Replacement:="", LookAt:=xlPart
    If WorksheetFunction.CountA(r.Rows(i)) = 0 Then Z.Rows(i).Delete
Next

我也在寻找空单元格和空格,并且仅在所有相应列均为空或没有任何实际数据的空格时才删除该行。

此代码中的问题是它也在Excel工作表中删除数据之间的空格。请提供建议,我该如何避免。

1 个答案:

答案 0 :(得分:0)

如果您不介意同时修改(修剪)了同时具有数据和前导/滞后空格的单元格,那么Siddharth的响应确实很简洁。另一个选择是检查单元格是否完全由空格组成...诸如

iRowLast = .Cells(.Rows.Count, iCol_Exp).End(xlUp).Row
Set r = .Range("A1:F48")
Set Z = ActiveSheet.Range("A1:Z50")

For i = iRowLast To 10 Step (-1)
    'r.Rows(i).Replace What:=Chr(32), Replacement:="", LookAt:=xlPart
    For Each vcell In r.Rows(i).Columns
        If Len(Replace(vcell, " ", "")) = 0 Then vcell = ""
    Next

    If WorksheetFunction.CountA(r.Rows(i)) = 0 Then Z.Rows(i).Delete
Next
相关问题