[英]Delete rows with multiple conditons using VBA
I have a raw data in excel as xls extention. 我在excel中有原始数据作为xls扩展。 I want to delete the rows based on 3 conditions.
我想基于3个条件删除行。
here is the data 这是数据
A B C D
Fname Lname activeconnect Clustername
1. ram raju Yes ExtIndia
2. John reynold Yes ExtBUD
3. Sree olaf No ExtUSA
4. consider as Blank row
5. alois lobmier No ExtPeru
6. Niko papa yes ExtItaly
The sample output should be as follows 样本输出应如下
A B C D
Fname Lname activeconnect Clustername
1. ram raju Yes ExtIndia
Here my code works for the first condition and also delete the row which has field with "ExtBUD". 在这里,我的代码适用于第一个条件,并且还删除了具有“ ExtBUD”字段的行。 can anyone help me please
有人可以帮我吗
Sub Import()
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
row_number = 1
Do
DoEvents
row_number = row_number + 1
active_connect = Sheets("WX Messenger Export 20150819").Range("U" & row_number)
cluster_name = Sheets("WX Messenger Export 20150819").Range("X" & row_number)
If InStr(active_connect, "No") >= 1 Then
If InStr(cluster_name, "ExtBUD") >= 1 And InStr(cluster_name, "ExtItaly") >= 1 Then
Sheets("WX Messenger Export 20150819").Rows(row_number & ":" & row_number).Delete
row_number = row_number - 1
End If
End If
Loop Until active_connect = ""
End Sub
In case of If Then
you have to use or
in the condition If Then
必须使用or
Sub Import()
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
row_number = 1
Do
DoEvents
row_number = row_number + 1
active_connect = Sheets("WX Messenger Export 20150819").Range("U" & row_number)
cluster_name = Sheets("WX Messenger Export 20150819").Range("X" & row_number)
If InStr(active_connect, "No") >= 1 or (InStr(cluster_name, "ExtBUD") >= 1 And InStr(cluster_name, "ExtItaly") >= 1) Then
Sheets("WX Messenger Export 20150819").Rows(row_number & ":" & row_number).Delete
row_number = row_number - 1
End If
Loop Until active_connect = ""
End Sub
but you still haven't check the blank rows. 但您仍然没有选中空白行。 therefore add the following part to the if condition too:
or (Sheets("WX Messenger Export 20150819").Range("C" & row_number) = "" and Sheets("WX Messenger Export 20150819").Range("D" & row_number) = "")
因此也将以下部分添加到if条件中:
or (Sheets("WX Messenger Export 20150819").Range("C" & row_number) = "" and Sheets("WX Messenger Export 20150819").Range("D" & row_number) = "")
The final code would be 最终的代码是
Sub Import()
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
row_number = 1
Do
DoEvents
row_number = row_number + 1
active_connect = Sheets("WX Messenger Export 20150819").Range("U" & row_number)
cluster_name = Sheets("WX Messenger Export 20150819").Range("X" & row_number)
If InStr(active_connect, "No") >= 1 or (InStr(cluster_name, "ExtBUD") >= 1 And InStr(cluster_name, "ExtItaly") >= 1) or (Sheets("WX Messenger Export 20150819").Range("C" & row_number) = "" and Sheets("WX Messenger Export 20150819").Range("D" & row_number) = "") Then
Sheets("WX Messenger Export 20150819").Rows(row_number & ":" & row_number).Delete
row_number = row_number - 1
End If
Loop Until active_connect = ""
End Sub
Try this one: 试试这个:
Public Sub deleteRow()
Dim row, lastRow As Integer
With Sheets("WX Messenger Export 20150819")
'Set the start row
row = 2
'Get the last row
lastRow = .Range("A2").SpecialCells(xlCellTypeLastCell).row
'loop from first row to last row
Do While row <= lastRow
'If condition are equal, delete row
If .Range("C" & row) = "No" Or .Range("D" & row) = "ExtBUD" Or .Range("D" & row) = "ExtItaly" Or (.Range("C" & row) = "" And .Range("D" & row) = "") Then
.Rows(row).Delete
lastRow = lastRow - 1
'Else go to next row
Else
row = row + 1
End If
Loop
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.