2010-03-05 16 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 2010-03-06 13:41:08

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 
相關問題