[英]Excel vba macro to delete rows in one sheet that do no match those in another using auto filter via input box
this is my first post, I am and novice user and would really like some help. 我是新手,这是我的第一篇帖子,真的很需要帮助。 I have been searching for a week to no avail. 我一直在找一个星期都没有用。 I have 2 Sheets (Sheet1 and Sheet2) with a small macro assigned to delete any rows in Sheet1 which do not match those in Sheet2. 我有2个工作表(Sheet1和Sheet2),分配了一个小宏以删除Sheet1中与Sheet2中不匹配的任何行。 I am also using a InputBox to capture the user defined criteria based on field 4, which works great and filters correctly. 我还使用InputBox来捕获基于字段4的用户定义的条件,该方法效果很好并且可以正确过滤。 My problem is related to the second part as the deletion is performed for matches across the whole of Sheet1 (ignoring the filter). 我的问题与第二部分有关,因为删除是针对整个Sheet1上的匹配项执行的(忽略过滤器)。 What I want is for the deletion to occur only on those rows filtered by by the user. 我想要的是仅在用户筛选的那些行上进行删除。 Any help would be gratefully received, if you could annotate your answers that would be helpful and apologies if I am making rookie mistakes. 如果您能注释您的答案(如果我犯了菜鸟错误,那将是有帮助的),将不胜感激,我们将不胜感激。
Sub DeleteRows()
'Deletes rows where one cell does not meet criteria
Dim ws1 As Worksheet: Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("Sheet2")
Dim criteria As String
Dim found As Range
Dim i As Long
Dim Area As String
Dim Data As Variant
'Auto filters by user selection
Area = InputBox("Enter your required Building(s) - comma separated")
If InStr(1, Area, ",") > 0 Then
Data = Split(Area, ",")
Range("A1").AutoFilter Field:=4, Criteria1:=Trim(Data(0)), Operator:=xlOr, Criteria2:=Trim(Data(1))
Else
Range("A1").AutoFilter Field:=4, Criteria1:=Area
End If
'Deletes all rows from Sheet1 that do not match
For i = 2 To 100
criteria = ws1.Cells(i, 1).Value
On Error Resume Next
Set found = ws2.Range("A:A").Find(What:=criteria, LookAt:=xlWhole)
On Error GoTo 0
If found Is Nothing Then
ws1.Cells(i, 1).EntireRow.Delete
i = i - 1
End If
Next i
End Sub
Try this :) 尝试这个 :)
The code checks if the row is hidden and only deletes the row if it isn't. 该代码检查该行是否被隐藏,如果没有隐藏,则仅删除该行。
Sub DeleteRows()
'Deletes rows where one cell does not meet criteria
Dim ws1 As Worksheet: Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("Sheet2")
Dim criteria As String
Dim found As Range
Dim i As Long
Dim Area As String
Dim Data As Variant
'Auto filters by user selection
Area = InputBox("Enter your required Building(s) - comma separated")
If InStr(1, Area, ",") > 0 Then
Data = Split(Area, ",")
Range("A1").AutoFilter Field:=4, Criteria1:=Trim(Data(0)), Operator:=xlOr, Criteria2:=Trim(Data(1))
Else
Range("A1").AutoFilter Field:=4, Criteria1:=Area
End If
'Deletes all rows from Sheet1 that do not match
For i = 2 To 100
criteria = ws1.Cells(i, 1).Value
On Error Resume Next
Set found = ws2.Range("A:A").Find(What:=criteria, LookAt:=xlWhole)
On Error GoTo 0
If found Is Nothing And ws1.Cells(i, 1).EntireRow.Hidden = False Then ' Checks if the row is hidden
ws1.Cells(i, 1).EntireRow.Delete
i = i - 1
End If
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.