我有这个VBA代码来删除excel中的行
Sub deleterows()
i = 1
Do Until i = 150000
If ActiveCell.Value = False Then
ActiveCell.EntireRow.Delete
End If
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop
End Sub
但是,此代码不会删除包含“False”值的所有行,我一直在尝试将其更改为activecell.value =“”和activecell.value = vbnullstring,但它仍然不会删除所有空白行
如果要删除行,则应从最后一行移到顶部。
此外,最好避免使用ActiveCell
。
Sub deleterows2()
i = 1
For i = 150000 To 1 Step -1
If Cells(i, 1).Value = False Or Cells(i, 1).Value = "False" Then
Rows(i).EntireRow.Delete
End If
Next i
End Sub
根据需要调整。我假设您的列A包含您正在检查的单元格。如果它是另一列,只需在Cells(i,1)
中使用该列的索引号。因此,如果您需要检查D列,请使用Cells(i,4)
您可以通过一个小的更改来修复它,如下所示:
If ActiveCell.Value = False Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Activate
End If
基本上,你应该只在值为!= False
时激活下一个卖出,否则它将跳过行。
这里有一些很好的东西,它们看起来像你想要完成的东西。
我假设150000基本上只是一个大数字,因此您确信所有使用的行都在考虑之中。
Sub DeleteRows()
i = 0
Do While ActiveCell.Offset(i, 0).Row <= ActiveCell.End(xlDown).Row
'This only considers used rows - much better than considering 15,000 rows even if you're only using 100
If ActiveCell.Offset(i, 0).Value <> 1 Then
'If you directly have a boolean value (i.e. 'True', 'False', or '0','1', you do not need to compare to another value. If your cells contain text, compare with the string in quotes (i.e. ...= "False")
ActiveCell.Offset(i, 0).Delete
Else: i = i + 1
End If
'Don't have to activate the next cell because we're referencing off of a fixed cell
Loop
End Sub