Excel 在工作簿VBA的第二个工作表上应用筛选器

Excel 在工作簿VBA的第二个工作表上应用筛选器,excel,vba,Excel,Vba,我已经有了一个过滤器宏来清理工作簿第一张工作表的数据,现在我想清理第二张工作表RSSI,我认为通过将第二张工作表声明为实际工作表可能会起作用,但它仍然在第一张工作表上起作用 Dim wsToFilter As Worksheet Dim wbToFilter As Workbook Set wbToFilter = Workbooks("2. Detalle_Transacciones_pendientes_rechazadas_MDM_27Ene20.xlsx") Set wsToFilter

我已经有了一个过滤器宏来清理工作簿第一张工作表的数据,现在我想清理第二张工作表RSSI,我认为通过将第二张工作表声明为实际工作表可能会起作用,但它仍然在第一张工作表上起作用

Dim wsToFilter As Worksheet
Dim wbToFilter As Workbook
Set wbToFilter = Workbooks("2. Detalle_Transacciones_pendientes_rechazadas_MDM_27Ene20.xlsx")
Set wsToFilter = wbToFilter.Worksheets("Rechazos_SSI_2019")

Dim RowToTest2 As Long

For RowToTest2 = Cells(Rows.Count, 2).End(xlUp).row To 2 Step -1

With Cells(RowToTest2, 1)
   If .Value <> "BATCH" _
   Then _
   Rows(RowToTest2).EntireRow.Delete
End With

Next RowToTest2

将其更改为autofilter并运行良好,我将Variatus编写的内容作为提示,因此我修改了代码,如下所示。

下面的代码首先查看一个工作表,然后查看另一个工作表。我以为这就是你想要的。它保留了确定要删除的行的方法

Dim wbToFilter As Workbook
Dim wsToFilter As Worksheet
Dim RowToTest2 As Long
Dim WsCounter As Integer

Set wbToFilter = Workbooks("2. Detalle_Transacciones_pendientes_rechazadas_MDM_27Ene20.xlsx")
' here the first worksheet is assigned to the variable WsToFilter
Set wsToFilter = wbToFilter.Worksheets("Rechazos_SSI_2019")
For WsCounter = 1 To 2
    With wsToFilter         ' all the following is executed on WsToFilter
                            ' observe the leading periods which create the link
        For RowToTest2 = .Cells(.Rows.Count, 2).End(xlUp).Row To 2 Step -1
            With .Cells(RowToTest2, 1)
               If .Value <> "BATCH" Then _
                Rows(RowToTest2).EntireRow.Delete
            End With
        Next RowToTest2
    End With
    ' now, for the second loop, the other worksheet is assigned
    ' to the variable WsToFilter
    Set wsToFilter = wbToFilter.Worksheets("Rechazos_RO_2019")
Next WsCounter

CellsRows.Count、2.EndxlUp.row到2步骤-1、CellsRowToTest2、1和RowsRowToTest2.EntireRow.Delete都在活动工作表上,因为没有为它们指定工作表。尝试使用wbToFilter.Cells.Rows.Count、2.EndxlUp.Row End并观察所有前导句点。指定其他单元格的工作表。因此,我将替换您提供给我的行的工作表?删除筛选项而不是在所有行中循环可能是个好主意,但我怀疑您的语法是否非常健壮。AutoFilter.Range.Offset1.EntireRow.Delete显示为仅删除一行。无论如何,在我看到这篇文章之前,我为你写了一个解决方案。既然已经准备好了,我就把它寄出去。也许你会发现它很有用。是的,细节是两张表之间的过滤器变化,它删除没有该值的行,只留下有该值的行,这就是我的想法,但你的回答给了我一个不同解决方案的想法,感谢您在这里投入的时间和您给我的知识:
Dim wbToFilter As Workbook
Dim wsToFilter As Worksheet
Dim RowToTest2 As Long
Dim WsCounter As Integer

Set wbToFilter = Workbooks("2. Detalle_Transacciones_pendientes_rechazadas_MDM_27Ene20.xlsx")
' here the first worksheet is assigned to the variable WsToFilter
Set wsToFilter = wbToFilter.Worksheets("Rechazos_SSI_2019")
For WsCounter = 1 To 2
    With wsToFilter         ' all the following is executed on WsToFilter
                            ' observe the leading periods which create the link
        For RowToTest2 = .Cells(.Rows.Count, 2).End(xlUp).Row To 2 Step -1
            With .Cells(RowToTest2, 1)
               If .Value <> "BATCH" Then _
                Rows(RowToTest2).EntireRow.Delete
            End With
        Next RowToTest2
    End With
    ' now, for the second loop, the other worksheet is assigned
    ' to the variable WsToFilter
    Set wsToFilter = wbToFilter.Worksheets("Rechazos_RO_2019")
Next WsCounter