我需要从Excel文件中删除行,该行的值与另一行净为零。 10,000行中的1,000行可能最终被删除。
我按绝对值从最大到最小的顺序对行进行排序(现在手动完成,以后可以添加),然后迭代这些值并删除净为零的行。
我已经编写了要在50行上执行的代码,但是它不起作用:
Sub delete_row()
Dim activerow As Integer
activerow = 2
Do
If ActiveSheet.Cells(activerow, 13).Select + ActiveCell.Offset(1, 0) = 0 Then
Rows("activerow:activerow + 1").Select
Selection.Delete Shift:=xlUp
Else: activerow = activerow + 1
End If
Loop Until activerow = 50
End Sub
我是VBA和编程新手。
答案 0 :(得分:1)
作为开始(注释中的解释):
Sub delete_row()
Dim iRow As Long ' in Excel sheet you can handle up to 1 million row
' Integer type reaches up to some 32 thousands
' Long type allows for far more
For iRow = Cells(Rows.Count, 13).End(xlUp).Row To 2 Step -1 ' step backwards from column M (column index 13) last not empty cell row index to 2
If Cells(iRow, 13).Value + Cells(iRow + 1, 13) = 0 Then Cells(iRow, 13).EntireRow.Delete Shift:=xlUp
Next
End Sub
删除行时,您始终必须从最后一行开始,然后循环到bakwards,以避免丢失任何行
您真的很需要Select
(请看This link
)