2010-03-05 3 views
0

VBA를 사용하여 프로그래밍 방식으로 한 워크 시트의 행을 복사 한 다음 이동 한 후에 어떻게 삭제합니까? 기준에 따라 모든 레코드를 삭제하는 것 같지 않습니다. 검색하고 있습니다.한 워크 시트의 행을 복사 한 다음 이동 한 후 삭제하는 방법은 무엇입니까?

Dim lRowCounter as Long, lTotalRows as Long,sCommPerUnit as String,lExceptionRowCounter as Long 

lTotalRows = 10 

For lRowCounter = 1 To lTotalRows 

    'If Row has no mapping, move it to Exceptions Report 
    sCommPerUnit = Trim(rRange.Offset(lRowCounter, 2)) 

    If (sCommPerUnit = "SOMETHING") Then 

     lExceptionRowCounter = lExceptionRowCounter + 1 
     'Move row to Exception Report Worksheet 
     rExceptionRange.Offset(lExceptionRowCounter, 1) = Trim(rRange.Offset(lRowCounter, Output_Order)) 

     'Delete Row from Report 
     rRange.Offset(lRowCounter, 1).EntireRow.Delete xlShiftUp 
    End If 
Next 

관련

고성

답변

0

범위의 행을 삭제할 때 마지막 행부터 시작하여 뒤로 작업하는 것이 좋습니다. 즉, 행을 삭제해도 아직 보지 않은 행에 대해서는 아무 것도 바뀌지 않습니다.

Dim lRowCounter as Long, lTotalRows as Long,sCommPerUnit as String,lExceptionRowCounter as Long 

lTotalRows = 10 

For lRowCounter = lTotalRows To 1 Step -1 

    'If Row has no mapping, move it to Exceptions Report 
    sCommPerUnit = Trim(rRange.Offset(lRowCounter, 2)) 

    If (sCommPerUnit = "SOMETHING") Then 

     lExceptionRowCounter = lExceptionRowCounter + 1 
     'Move row to Exception Report Worksheet 
     rExceptionRange.Offset(lExceptionRowCounter, 1) = Trim(rRange.Offset(lRowCounter, Output_Order)) 

     'Delete Row from Report 
     rRange.Offset(lRowCounter, 1).EntireRow.Delete xlShiftUp 
    End If 
Next lRowCounter 
+0

감사합니다. – Kojof

2

그런 다음 위로 이동 첫 번째 행을 삭제하지만, 다음 행으로 증가하고 있습니다. 의미, 당신은 행을 건너 뛰고 있습니다. 나는 항상 첫 번째 행을 삭제하고 위로 이동합니다. 다음 루프에서 다음 행이 첫 번째 행이됩니다.

'If Row has no mapping, move it to Exceptions Report 
    sCommPerUnit = Trim(rRange.Offset(0, 2)) 

    ... 

    'Move FIRST row to Exception Report Worksheet'S LAST ROW 
    rExceptionRange.Offset(lExceptionRowCounter, 1) = Trim(rRange.Offset(0, Output_Order)) 

    'Delete FIRST Row from Report 
    rRange.Offset(0, 1).EntireRow.Delete xlShiftUp 
관련 문제