excel - 根据特定列中的文本值删除行
问题描述
我写了一个简短的宏来删除我的工作簿的“预算”选项卡中 I 列中值为“不适用”的所有行。
当我通过测试运行宏时,它似乎没有做任何事情:
Sub Remove_NA_Macro_Round_2()
With Sheets("Budget") 'Applying this macro to the "Budget" sheet/tab.
'Establishing our macro range parameters
Dim LastRow As Long
Dim i As Long
'Setting the last row as the ending range for this macro
LastRow = .Range("I50").End(xlUp).Row
'Looping throughout all rows until the "LastRow" ending range set above
For i = LastRow To 1 Step -1
If .Range("I" & i).Value = "Not Applicable" Then
.Range("I" & i).EntireRow.Delete
End If
Next
End With
End Sub
我很感激任何帮助!
解决方案
您实际上并没有引用With Sheets("Budget")
. .
在 的每个实例之前添加一个句点Range
,否则会有一个隐含的ActiveSheet
,它不一定是“预算”选项卡。
With Sheets("Budget")
...
LastRow = .Range("I50").End(xlUp).Row
...
If .Range("I" & i).Value = "Not Applicable" Then
.Range("I" & i).EntireRow.Delete
End If
...
End With
编辑:
根据评论和您提供的屏幕截图,更改LastRow
确定方式(摆脱硬编码I50
):
LastRow = .Cells(.Rows.Count, "I").End(xlUp).Row
推荐阅读
- reactjs - 涉及 Redux、useContext 和 HOC 的架构有哪些缺陷和性能问题?
- neo4j - 从 CSV 加载时如何创建节点、创建关系和删除属性?
- nlp - Powerbi on Prem 上的 NLP
- paypal - paypal 订单号的有效期是多久?
- android - ExoPlayer 无法播放分辨率为 1080p 的直播流
- python - ndarray.shape 返回什么?
- php - PHP foreach 循环从 mysql 创建 JSON
- spring - 如果有 @Bean-method 证明单个 bean,则 @Bean-method 提供列表将被忽略
- android - 对象在作为 Parcelable 对象传递后为空
- javafx - 使用 Sublime Text 3 编译 JavaFX 时出现问题