如何删除包含非数字值的所有行

时间:2018-04-13 08:46:35

标签: vba excel-vba excel

我有一张表格,看起来像是填充的列..

                       Column B     Column C     Column D
row 2 : Product 1      100          200          300
row 3 : Product 2      100          200A         100
row 4 : Product 3      AAA          200          300
row 5 : Product 4      600          200          300
row 6 : Product 5      150          200          300,2A

我想要的是: 对于每一行,如果columnB的值不是数字,或者columnC的值不是数字或列D的值不是数字,则删除该行。

这是我的代码,对我来说很好看:

Dim xWs As Worksheet
Set xWS = ThisWorkbook.Sheets("sheet1")
lastrow = xWs.Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To lastrow
    BValue = xWs.Range("B" & i).Value
    CValue = xWs.Range("C" & i).Value
    DValue = xWs.Range("D" & i).Value
    If Not IsNumeric(BValue) Or Not IsNumeric(CValue) Or Not IsNumeric(DValue) Then
         xWs.Rows(i).Delete
    End If
Next i

我的问题是:当我执行代码时..删除第4行和第6行,但是当第b行值不是数字时,请保留第3行的行。

我无法弄清楚问题.. 提前谢谢..

2 个答案:

答案 0 :(得分:2)

您可以一次性使用Union并删除,以避免以向上方式删除行来跳过行。你应该删除倒退的行。

Option Explicit

Sub test()

Dim xWs As Worksheet, i As Long, lastRow As Long, unionRng As Range
Set xWs = ThisWorkbook.Sheets("sheet1")
lastRow = xWs.Cells(xWs.Rows.Count, 2).End(xlUp).Row
Dim BValue As Variant, CValue As Variant, DValue As Variant

For i = 2 To lastRow
    BValue = xWs.Range("B" & i).Value
    CValue = xWs.Range("C" & i).Value
    DValue = xWs.Range("D" & i).Value
    If Not IsNumeric(BValue) Or Not IsNumeric(CValue) Or Not IsNumeric(DValue) Then
        If Not unionRng Is Nothing Then
            Set unionRng = Union(unionRng, xWs.Rows(i))
        Else
            Set unionRng = xWs.Rows(i)
        End If

    End If
Next i

If Not unionRng Is Nothing Then unionRng.Delete

End Sub

或倒退:

Option Explicit

Sub test()

Dim xWs As Worksheet, i As Long, lastRow As Long, unionRng As Range
Set xWs = ThisWorkbook.Sheets("sheet1")
lastRow = xWs.Cells(xWs.Rows.Count, 2).End(xlUp).Row
Dim BValue As Variant, CValue As Variant, DValue As Variant

For i = lastRow To 2 Step -1
    BValue = xWs.Range("B" & i).Value
    CValue = xWs.Range("C" & i).Value
    DValue = xWs.Range("D" & i).Value
    If Not IsNumeric(BValue) Or Not IsNumeric(CValue) Or Not IsNumeric(DValue) Then xWs.Rows(i).Delete
Next i

End Sub

答案 1 :(得分:1)

这是一种不会循环遍历行的最快方式...它使用SpecialCellsAutofilter

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range, delRng As Range, totRng As Range
    Dim lRow As Long
    Dim i As Long

    '~~> Change this to the relevant sheet
    Set ws = Sheet1

    With ws
        '~~> Find the last row
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        Set rng = .Range("B2:D" & lRow)
        Set totRng = .Range("B1:D" & lRow)

        On Error Resume Next
        Set delRng = rng.SpecialCells(xlCellTypeConstants, 2)
        On Error GoTo 0

        If delRng Is Nothing Then Exit Sub _
        Else delRng.ClearContents

        For i = 1 To 3
            .AutoFilterMode = False
            With totRng
                .AutoFilter Field:=i, Criteria1:="="
                .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End With
            .AutoFilterMode = False
        Next i
    End With
End Sub

我的假设(适用时更改)

  1. 第1行有标题
  2. Col B,C,D具有相同的最后一行
  3. 代码表的名称为Sheet1
  4. 视觉说明

    enter image description here