Excel:如果某些列中的单元格为空,则删除行?


23

我是Excel的新手,所以如果这样做很容易,请原谅。我已经查看了很多选项,但是找不到我需要的东西。

基本上,我想删除C列中所有不包含值的行。我该怎么做?

我现在手动处理5000多种产品,这让我发疯。


2
为什么在将带有VBA解决方案的Stack Overflow上迁移到超级用户时提出问题?
brettdj

Answers:


31

如果单元格确实是空白的,则可以非常快速地执行此操作 SpecialCells

手册

  • 选择列C
  • F5,然后Special
  • 选中Blanks,然后OK(请参阅底部图片中的此步骤)
  • 删除现在选中的行(例如,右键单击选择> 删除单元格... > 整行或通过功能区(请参见第二张屏幕截图))

VBA

Sub QuickCull()
    On Error Resume Next
    Columns("C").SpecialCells(xlBlanks).EntireRow.Delete
End Sub

屏幕快照显示“转到特殊菜单”->“空白”菜单 屏幕截图显示了如何使用功能区删除选择中的整个行


8

这是一种简单的手动方法

  1. 将一个Auto Filter应用于您的工作表
  2. C空白列上过滤
  3. 选择所有可见行
  4. 删除行
  5. 移除过滤器

如果需要,可以使用VBA自动执行此过程。尝试运行宏记录器以开始使用


简单而有效。工作奇迹!
Tiana987642

2

我认为假设您在其他单元格中没有一堆其他公式的最简单方法是按C列对所有内容进行排序,然后删除C列具有空白的所有行(sort函数将放置空白值对于文件顶部的C列)。

综上所述:

  • 单击标记为“ 1”的单元格上方和标记为“ A”的单元格左侧的折叠纸单元格(突出显示所有)
  • 单击数据,然后排序
  • 按C列排序,并让最小值成为第一位
  • 只需向下突出显示行,直到用C列的值打到第一行,然后删除突出显示的所有内容

0

这应该工作。

Columns("C:C").Select
Set rngRange = Selection.CurrentRegion
lngNumRows = rngRange.Rows.Count
lngFirstRow = rngRange.Row
lngLastRow = lngFirstRow + lngNumRows - 1
lngCompareColumn = ActiveCell.Column
For lngCurrentRow = lngLastRow To lngFirstRow Step -1
If (Cells(lngCurrentRow, lngCompareColumn).Text = "") Then _
Rows(lngCurrentRow).Delete
Next lngCurrentRow

-1

您可以将以下代码放在工作表模块中(右键单击工作表标签并选择“查看代码”):

Sub Delete_Blank_Rows()


'Deletes the entire row within the selection if the ENTIRE row contains no data.

'We use Long in case they have over 32,767 rows selected.

Dim i As Long
Dim LastRow As Integer

'We turn off calculation and screenupdating to speed up the macro.

 With Application
     .Calculation = xlCalculationManual
     .ScreenUpdating = False

     'Reduce the work on the calc and define the range

     LastRow = Range("A" & Rows.Count).End(xlUp).Row
     Range("A2:A" & LastRow).Select

     'We work backwards because we are deleting rows.

     For i = Selection.Rows.Count To 1 Step -1
          If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
              Selection.Rows(i).EntireRow.Delete
          End If
     Next i

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With

End Sub

1
这会删除整行为空白的行吗?-如果包含,则类似于@drnewman答案。问题特别指出:如果某些列中的单元格为空,则删除行。
Davidenko

欢迎来到超级用户!请仔细阅读问题。您的答案没有回答原始问题。
DavidPostill
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.