Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 删除空行将生成1004错误_Vba_Excel - Fatal编程技术网

Vba 删除空行将生成1004错误

Vba 删除空行将生成1004错误,vba,excel,Vba,Excel,如果某列中的单元格为空,我想删除行 我正在使用我找到的示例代码 最初,有问题的单元格位于B列。我使用了以下内容: Sub delrowEmptyStr() Dim EmpCol As Range, LstRw As Long LstRw = Columns(2).Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row Set EmpCol = Ce

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

我正在使用我找到的示例代码

最初,有问题的单元格位于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
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
我重新格式化并将该数据放入A列。我将Sub更改为以下内容:

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
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:未找到单元格”

点击“调试”将突出显示该行

.SpecialCells(xlConstants).EntireRow.Delete
在运行Sub的第二次迭代之前,我确实对数据进行了不同的排序,我想知道是否可能是因为我想将所有空单元格都放在列的底部

我在想,也许我想把所有的空单元格都放在专栏的底部会是个问题吗

是的,当然,因为声明

LstRw
作为该特定列中的最后一个非空行。您只需在整个工作表中查找最后一个非空行,而不是在特定列中查找最后一个非空行,即可解决此问题:

'      vvvvvv
LstRw = Cells.Find(What:="*", SearchOrder:=xlRows, _
   SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
我在想,也许我想把所有的空单元格都放在专栏的底部会是个问题吗

是的,当然,因为声明

LstRw
作为该特定列中的最后一个非空行。您只需在整个工作表中查找最后一个非空行,而不是在特定列中查找最后一个非空行,即可解决此问题:

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

啊!!非常感谢你,我想这应该很简单,但无法找出原因。谢谢你的努力!啊!!非常感谢你,我想这应该很简单,但无法找出原因。谢谢你的努力!