【发布时间】:2017-03-08 07:39:22
【问题描述】:
我是 VBA 新手,我正在尽力解释我想要做什么
我需要检查表 1 和表 2 如果他们在行中有值“AAA”或“BBB”或“CCC”,我想保留它, 如果没有,删除整行
我下面的代码只能帮助我删除行,除非它在 Q 列中包含“AAA”
我不知道如何添加更多值,例如“BBB”和“CCC”,如果行有这些值,我想保留它
如何添加更多列进行检查?现在只检查 Q 列,如果我想从 H 列检查到 R 吗?
我其实有10个值(AAA, BBB, CCC .... JJJ) 想保留,需要一个一个打出来,还是有方法问excel 检查列表,如果表 1 和表 2 中的任何单元格与任何匹配 这 10 个值中的一个,保留该行,否则,删除整个 行
列表位于 A1 列的第 3 页:A10
谢谢! 我的代码如下
Sub RemoveCell()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With Sheets("Sheet1")
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "Q")
If Not IsError(.Value) Then
If .Value <> "AAA" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
【问题讨论】:
-
就像循环遍历行一样,您也应该遍历列。首先定义包含数据的最后一列,然后单步执行。要添加 BBB 和 CCC,您应该查看 IF 语句中的 OR 运算符。
-
欢迎来到 SO,请拨打tour(点击它)了解这个社区的运作方式! ;)