使用VBA删除Excel中的空行

发布于 2024-12-11 17:44:29 字数 176 浏览 0 评论 0 原文

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

worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

上面的代码工作正常,但给出运行时错误'1004':未找到单元格。

I am trying to delete Empty rows by using below code:

worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

The code above is working fine, but giving run time error '1004': No Cells were found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆追雨的时光 2024-12-18 17:44:29
On Error Resume Next
worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

当没有空白单元格时,错误处理会有所帮助。如果没有这样的单元格,SpecialCells(xlCellTypeBlanks) 将始终返回错误,因此如果您想使用 SpecialCells( xlCellTypeBlanks)

On Error Resume Next
worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

The error handling helps when there are no blank cells. SpecialCells(xlCellTypeBlanks) will always return an error if there are no cells like that so error handling is the only way (that I know of) to deal with it if you want to use SpecialCells(xlCellTypeBlanks).

情徒 2024-12-18 17:44:29

您需要测试是否有任何空白。

If WorksheetFunction.CountBlank(Worksheet.Columns("A:A")) > 0 Then
    Worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If

如果没有空格,您可以使用 On Error Resume Next 跳过该行,但通常最好测试特定条件,而不是假设您知道错误是什么。

据我所知,如果 A 列中的每个单元格都有值,您只会收到“未找到单元格”消息。

编辑:根据 @brettdj 的评论,这里有一个仍然使用 CountBlank 的替代方案:

If WorksheetFunction.CountBlank(Intersect(worksheet.UsedRange, ws.Columns("A:A"))) > 0 Then
    worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If

当然,UsedRange 是出了名的变化无常,并且可能比看起来更大。我认为最好首先确定要删除行的实际范围,然后检查该范围内的特殊单元格,例如:

Sub DeleteRows()
Dim ws As Excel.Worksheet
Dim LastRow As Long

Set ws = ActiveSheet
LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
With ws.Range("A2:A" & LastRow)
    If WorksheetFunction.CountBlank(.Cells) > 0 Then
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
End With
End Sub

最后一点 - 我将变量从“worksheet”更改为“ws”,因为“worksheet”是Excel 保留字。

You need to test that there are any blanks.

If WorksheetFunction.CountBlank(Worksheet.Columns("A:A")) > 0 Then
    Worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If

You can just use On Error Resume Next to skip over the line if there are no blanks, but it's generally preferable to test for a specific condition, rather than assuming you know what the error will be.

As far as I can see you'd only get the "No Cells Found" message if every cell in Column A has a value.

EDIT: Based on @brettdj's comments, here's an alternative that still uses CountBlank:

If WorksheetFunction.CountBlank(Intersect(worksheet.UsedRange, ws.Columns("A:A"))) > 0 Then
    worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If

Of course UsedRange is notoriously fickle and may be bigger than it appears. I think it's best to first determine the actual range where the rows are to be deleted and then check the SpecialCells in that range, e.g.:

Sub DeleteRows()
Dim ws As Excel.Worksheet
Dim LastRow As Long

Set ws = ActiveSheet
LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
With ws.Range("A2:A" & LastRow)
    If WorksheetFunction.CountBlank(.Cells) > 0 Then
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
End With
End Sub

One last note - I changed the variable from "worksheet" to "ws" as "worksheet" is an Excel reserved word.

冷默言语 2024-12-18 17:44:29

和我一起工作很好。这些声明不会向我抛出任何错误

 Sheet1.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'perfect

我找到了您问题的这些类型的解决方案

 On Error Resume Next
 Sheet1.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 On Error GoTo 0

看看这些链接

http://www.excelforum.com/excel-programming/390329-microsoft-visual-basic-run-time-error-1004-no-cells-were-found.html

< a href="http://www.mrexcel.com/forum/showthread.php?t=343744" rel="nofollow">http://www.mrexcel.com/forum/showthread.php?t=343744

是的,您设置了对象吗?工作表在这里没有任何意义

dim wsheet as worksheets
set wsheet = worksheets("worksheetname") or worksheets("sheet1")
wsheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

Working fine with me . These statement does not throw any error to me

 Sheet1.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'perfect

I found these type of solution for your question

 On Error Resume Next
 Sheet1.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 On Error GoTo 0

Take a look at these links

http://www.excelforum.com/excel-programming/390329-microsoft-visual-basic-run-time-error-1004-no-cells-were-found.html

http://www.mrexcel.com/forum/showthread.php?t=343744

and well yes did you set your object ? worksheet does not make any sense here

dim wsheet as worksheets
set wsheet = worksheets("worksheetname") or worksheets("sheet1")
wsheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
无尽的现实 2024-12-18 17:44:29

另一种方式:

If Range("Table2").Rows.Count > 1 Then
   Range("Table2").EntireRow.Delete
End If

Another way:

If Range("Table2").Rows.Count > 1 Then
   Range("Table2").EntireRow.Delete
End If
吃→可爱长大的 2024-12-18 17:44:29
Sub delete_rows_blank()

t = 1
lastrow = ActiveSheet.UsedRange.Rows.Count
Do Until t = lastrow
If Cells(t, "A") = "" Then
Rows(t).Delete
End If
t = t + 1
Loop

End Sub
Sub delete_rows_blank()

t = 1
lastrow = ActiveSheet.UsedRange.Rows.Count
Do Until t = lastrow
If Cells(t, "A") = "" Then
Rows(t).Delete
End If
t = t + 1
Loop

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文