2014-09-08 4 views
1

특정 열의 값을 검사하는 VBA Excel 코드가 있습니다. 해당 열의 행에 'Delete'값이 포함 된 경우 행을 삭제합니다.조건에 따라 행을 제거하는 VBA 느린 프로세스

코드는 제대로 작동하지만 실제로 느립니다. 코드를 빠르게 실행하는 방법에 대한 아이디어가 있습니까?

Dim rng1 As Range 
    Dim i As Integer, counter As Integer 
    'Set the range to evaluate to rng. 
    Set rng1 = Range("g1:g1000") 
    'initialize i to 1 
    i = 1 
    'Loop for a count of 1 to the number of rows 
    'in the range that you want to evaluate. 
    For counter = 1 To rng1.Rows.Count 
     'If cell i in the range1 contains an "Delete" 
     'delete the row. 
     'Else increment i 
     If rng1.Cells(i) = "Delete" Then 
      rng1.Cells(i).EntireRow.Delete 
     Else 
      i = i + 1 
     End If 
    Next 

감사

다.

답변

1
Sub deletingroutine() 
    Dim r As Range 
    For Each r In Range("g1:g1000") 
    If r = "delete" Then r.EntireRow.Delete 

    Next r 
End Sub 
+0

감사합니다. Andy,하지만 코드에 문제가 있습니다. 값이 'delete'인 두 개의 후속 항목이있는 경우 첫 번째 항목 만 detet됩니다. 나는 비슷한 것을 시도했다고 생각한다. 어쩌면 Excel 버전 때문일 수도 있습니다. Office 2010을 사용하고 있습니다. – Selrac

0

나는 자동 필터 기능으로 해결책을 찾을 수있었습니다.

Selection.AutoFilter 
    Set ws = ActiveWorkbook.Sheets("UploadSummary") 
    lastRow = ws.Range("G" & ws.Rows.Count).End(xlUp).Row 
    Set rng = ws.Range("G1:G" & lastRow) 
    ' filter and delete all but header row 
    With rng 
     .AutoFilter Field:=7, Criteria1:="delete" ' 7 refers to the 7th column 
     .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete 
    End With 
0

후 한 행동에 모두 표시 ("삭제") 행을 삭제 (collumn의 G에) 행을 정렬하려고 누군가에게 도움이되기를 바랍니다. 그것은 훨씬 빠릅니다.

관련 문제