如果值大于或小于?则删除整行?

时间:2013-07-23 15:27:28

标签: excel vba excel-vba

我是VBA for excel的新手,我遇到了一个小问题。如果C列中的值大于40000或小于-40000(这些是数据异常值),我必须删除整行。数据列表长达数千行,因此我需要继续操作,直到C列中的数据结束。任何帮助都会很棒!

2 个答案:

答案 0 :(得分:3)

此代码将遍历列C中的所有单元格,并删除任何超出指定范围的行

Sub DeleteRows()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim i As Long
    For i = Range("C" & Rows.Count).End(xlUp).Row To 1 Step -1
        If Not (Range("C" & i).Value > -40000 And Range("C" & i).Value < 40000) Then
            Range("C" & i).EntireRow.Delete
        End If
    Next i
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

答案 1 :(得分:1)

单向;

dim rows as Range, cell as Range, value As long

set cell = Range("c1")
do until cell.value = ""
    value = val(cell.value)
    if (value < -40000 or value > 40000) then
        if rows is Nothing then
            set rows = cell.EntireRow
        else
            set rows = Union(cell.EntireRow, rows)
        end if
    end if
    set cell = cell.Offset(1)
loop

if not rows is Nothing then rows.Delete