2013-07-23 199 views
-2

我是新來的VBA excel和我堅持一個小問題。如果列C中的值大於40000或小於-40000(這些是數據異常值),我必須刪除整行。數據列表是數千行,因此我需要繼續操作,直到列C中的數據結束。任何幫助將是偉大的!如果值大於或小於?,則刪除整行

+0

您可以錄製一個宏,輕鬆編輯它產生做到這一點的代碼.. – chancea

+0

太多重複列表:http://stackoverflow.com/search?q=[excel]+delete+row – SeanC

+0

可能的重複[有效的方法來刪除整個行,如果單元格不包含'@']( http://stackoverflow.com/questions/16901436/efficient-way-to-delete-entire-row-if-cell-doesnt-contain) – SeanC

回答

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 
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 
+0

+1爲'Rows.Count).end(xlU p).Row)'和'Calculation'和'ScreenUpdating'比我的更好的解決方案 – chancea