作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编程检查一个范围,从单元格范围中删除内容,然后删除已清除内容的整行单元格。目前,我运行代码,所有行都被删除。另外,我很欣赏使代码干燥的建议。
Option Explicit
Sub Alfredo()
Dim msg As String
Dim VarCase As Range
Dim ws As Worksheets
Set ws = Sheets("Data")
For Each VarCase In ws.Range("D1:D11000")
If VarCase.Value2 = "John" Or VarCase.Value2 = "Thompson" Or VarCase.Value2 =
"Mattson" Then
VarCase.ClearContents
End If
Next VarCase
For Each VarCase In ws.Range("D1:D11000")
If VarCase.Value = "" Then
Rows.EntireRow.Delete
End If
Next VarCase
End Sub
最佳答案
在最后的 For 循环中,您有
Rows.EntireRow.Delete
你应该在哪里
VarCase.EntireRow.Delete
这可能是一般删除的原因。
For Each 构造并不总是能很好地处理正在更改的范围(此处为行删除),因此请注意这一点。您可能会通过 Union 累积一系列删除目标,并在最后的一条语句中删除以实现 DRYness,而无需清除任何内容。
此外,缩进是你的 friend 。
<小时/>编辑以添加 Union 方法的说明:
Sub TestRowDelete()
Dim ARange As Range
Dim DRange As Range
Set DRange = Nothing
For Each ARange In ActiveSheet.UsedRange.Rows
If ARange(1).Value = "d" Then ' testing first cell on each row
If DRange Is Nothing Then
Set DRange = ARange
Else
Set DRange = Union(DRange, ARange)
End If
End If
Next ARange
If Not DRange Is Nothing Then DRange.EntireRow.Delete
End Sub
关于vba - Excel VBA 从单元格中删除内容然后删除总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186443/
如何在表格底部做一行求和(TreeTableView - JavaFX 或 TableView)? (对不起我的英语不好)请写一个例子。 以图片(总计)为例 http://i.stack.imgur.
我是一名优秀的程序员,十分优秀!