如何使用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 :(得分: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
答案 1 :(得分: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