简体   繁体   English

Excel VBA宏可通过输入框使用自动筛选功能删除一个工作表中与另一工作表不匹配的行

[英]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.

相关问题 使用 vba 过滤和删除 excel 中的行 - 宏 - Filter and delete rows in excel using vba - macro Excel宏基于另一个工作表中的花名删除工作表中的行 - Excel Macro to delete rows in a sheet based on a roster in another sheet 通过下拉列表将数据从一张纸导出到另一张纸-VBA Excel Macro - Export data from one sheet to another via drop down list - VBA Excel Macro VBA 宏 - 删除列中不存在于另一列/工作表中的行 - VBA Macro - Delete rows in Column that arent present in another column/sheet Excel VBA宏:如何删除除一张纸之外的所有纸? - Excel VBA Macro: How do I delete all sheets except one sheet? Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet Excel - 如何删除1张表中不包含来自另一张表的列输入的所有行 - Excel - How to Delete All rows from 1 sheet that do not contain column input from another sheet Excel VBA二进制搜索以将一个工作表中的列与另一个工作表中的列进行比较,如果匹配则删除整行 - Excel VBA binary search to compare columns in one sheet to columns in another and delete the entire row if they match Excel 将内容从一张纸复制到另一张纸,无需 VBA/宏 - Excel copy contents from one sheet to another without VBA/Macro Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM