我是新来的VBA,我想我最好解释一下我想要做的Excel中 - 删除行,如果不包含在列表中值的一个
我需要检查表1和表2 如果他们在行中有“AAA”或“BBB”或“CCC”的值,如果不是,则删除整行
我的下面的代码只能帮我删除行, AAA” Q列
我不知道如何添加更多的值,如“BBB”&“CCC”,如果该行有这些值,任一个,我想保留它
如何添加更多列来检查?现在只是在列Q中检查,如果我想从列H检查它到R?
我实际上有10个值(AAA,BBB,CCC .... JJJ)想要保留,是否需要逐个输入,或者有一种方法要求excel 检查列表中,如果在表1,并与任何 一个从这些10个值匹配的表2中的任何细胞,保留该行,否则,删除整个 行
列表是在表3从塔A1定位: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运算符。 – Luuklag
欢迎来到SO,请参加[旅游](点击它)了解这个社区如何运作! ;) – R3uK