简体   繁体   English

如果选定行中的所有单元格为空白,则删除行

[英]Delete rows IF all cells in selected ROW is BLANK

I have financial data where some rows are blank and id like to be able to delete the entire row IF entire rows in a selected range are blank (its important for it to be in selected range as I might have "Revenues" in column A but then I have column BD be blank data (no numbers basically)).我有财务数据,其中一些行是空白的,如果选定范围内的整行都是空白的,我希望能够删除整行(它在选定范围内很重要,因为我可能在 A 列中有“收入”但是然后我的 BD 列是空白数据(基本上没有数字))。

I'd like for it to apply to a selected range, instead of having a predetermined range in the code (for the code to be flexible).我希望它适用于选定的范围,而不是在代码中具有预定范围(以使代码灵活)。

I am trying to use this format but it doesnt seem to be working:我正在尝试使用这种格式,但它似乎不起作用:

Sub deleteBlankRows()
Dim c As Range

On Error Resume Next
For Each c In Selection.Row.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Next
End Sub

Any thoughts would be welcome.任何想法都会受到欢迎。

Loop trough each complete row of selection and check if the count of blank cells matchs the count of all cells in row:循环遍历每个完整的选择行,并检查空白单元格的数量是否与行中所有单元格的数量相匹配:

在此处输入图像描述

My code:我的代码:

Dim rng As Range

For Each rng In Selection.Rows
    If Application.WorksheetFunction.CountBlank(rng) = rng.Cells.Count Then rng.EntireRow.Delete
Next rng

After executing code:执行代码后:

在此处输入图像描述

The emtpy row is gone空行不见了

UPDATE:更新:

@VasilyIvoyzha is absolutely right. @VasilyIvoyzha 是绝对正确的。 For each won't work properly on this situation. For each在这种情况下都无法正常工作。 A better approach would be:更好的方法是:

Dim i&, lastRow&

lastRow = Selection.SpecialCells(xlLastCell).Row

For i = lastRow To Selection.Cells(1).Row Step -1
    If WorksheetFunction.Concat(Rows(i)) = "" Then Rows(i).Delete
Next i

This way will delete empty rows on selection, even if they are consecutive.这种方式将在选择时删除空行,即使它们是连续的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM