且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果更改列中的单元格为空,则删除行

更新时间:2023-02-04 19:28:35

如果公式返回的零长度字符串不足以将公式结果还原为它们的值.您需要使用 Range.TextToColumns方法快速清除该列,使用固定宽度"并将列的值返回到其原始单元格,以使单元格真正为空白.

If you have zero-length strings returned by formulas, it is not sufficient to revert the formula results to their values. You need to quickly sweep the column with a Range.TextToColumns method, using Fixed Width and returning the column's values back to their original cells to make the cells truly blank.

Sub DeleteCol()
    Dim iCOL As Long, sFND As String

    With ActiveSheet
        With .Range(.Cells(1, 1), .Cells(1, 1).SpecialCells(xlLastCell))
            .Value = .Value
        End With
        sFND = "Approve"

        If CBool(Application.CountIf(.Rows(1), sFND)) Then
            iCOL = Application.Match(sFND, .Rows(1), 0)
            If CBool(Application.CountBlank(.Columns(iCOL))) Then
                With .Columns(iCOL)
                    .TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _
                                   FieldInfo:=Array(0, 1)
                    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                End With
            End If
        End If
    End With

End Sub

工作表的 COUNTBLANK函数会将零长度字符串计入其空白计数,以便我们在继续操作之前确定是否存在空白单元格.使用 COUNTIF函数,以确保第一行中的标题为"Approve".

The worksheet's COUNTBLANK function will count zero-length strings in its blank count so we can determine whether there are blank cells before proceeding. The same goes for using the COUNTIF function to make sure that there is a column header with 'Approve' in the first row.