删除空行会生成1004错误

如果某列中的单元格为空,我想删除行。

我正在使用我在这里find的示例代码。

最初,所讨论的细胞在B栏中。我使用了以下内容:

Sub delrowEmptyStr() Dim EmpCol As Range, LstRw As Long LstRw = Columns(2).Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row Set EmpCol = Cells.Find(What:="*", SearchOrder:=xlColumns, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Offset(, 1).EntireColumn With Intersect(Rows("2:" & LstRw), EmpCol) .FormulaR1C1 = "=IF(RC2="""",""X"","""")" .Value = .Value .SpecialCells(xlConstants).EntireRow.Delete End With End Sub 

我重新格式化并将这些数据放在列A中。我将Sub更改为以下内容:

 Sub delrowEmptyStr() Dim EmpCol As Range, LstRw As Long LstRw = Columns(1).Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row Set EmpCol = Cells.Find(What:="*", SearchOrder:=xlColumns, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Offset(, 1).EntireColumn With Intersect(Rows("2:" & LstRw), EmpCol) .FormulaR1C1 = "=IF(RC1="""",""X"","""")" .Value = .Value .SpecialCells(xlConstants).EntireRow.Delete End With End Sub 

现在运行这将产生“ 运行时错误1004:没有find单元格 ”。

点击“debugging”突出显示该行

 .SpecialCells(xlConstants).EntireRow.Delete 

我做了不同的sorting我的数据,然后运行第二次迭代 – 我想知道是否有我想摆脱到列的底部的所有空单元格可能是问题?

我想知道是否所有的空单元格我想摆脱到列的底部可能是问题?

是的,肯定是因为这个说法

 LstRw = Columns(1).Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row 

生成LstRw作为该特定列中的最后一个非空行。 您可以通过简单地find整个工作表中的最后一个非空行而不是特定列中的最后一个非空行来解决这个问题:

 ' vvvvvv LstRw = Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row