简体   繁体   English

Excel VBA 排除数据的高级过滤器

[英]Excel VBA advanced filter to exclude data

I'm filtering data in a table based on customer reference numbers.我正在根据客户参考编号过滤表格中的数据。 Is there a VBA code I could use to filter out rows based on their customer reference number?是否有 VBA 代码可用于根据客户参考号过滤掉行?

I'm using a very handy code I found online that includes data based on customer reference number that looks like this:我正在使用我在网上找到的一个非常方便的代码,其中包含基于客户参考编号的数据,如下所示:

Dim rgData As Range, rgCriteria As Range, rgOutput As Range

Set rgData = ThisWorkbook.Worksheets("Sheet 1").Range("A1").CurrentRegion
Set rgCriteria = ThisWorkbook.Worksheets("Sheet 1").Range("I1").CurrentRegion
Set rgOutput = ThisWorkbook.Worksheets("Sheet 2").Range("A1")

rgData.AdvancedFilter xlFilterCopy, rgCriteria, rgOutput

The code works by including only the customers found in the table that starts in cell "I1" in sheet 1, it then goes to the table that starts in celle "A1" and only includes customers that have the customer reference numbers and then sends the table to sheet 2.该代码的工作原理是仅包括在工作表 1 的单元格“I1”开始的表格中找到的客户,然后转到以单元格“A1”开始的表格,并且仅包括具有客户参考号的客户,然后发送表到表 2。

Is there a way that I could change the above code so that it excludes those customer reference numbers found in the table in cell "I1"?有没有一种方法可以更改上面的代码,以便它排除在单元格“I1”的表格中找到的那些客户参考号?

Thanks,谢谢,

Sub advanced_filter()

    Dim rgData As Range, rgCriteria As Range, rgOutput As Range
    
    With ThisWorkbook.Worksheets("Sheet 1")
        Set rgData = .Range("A1").CurrentRegion
        Set rgCriteria = .Range("D1").CurrentRegion
        Set rgOutput = .Range("F1")
    
        .Range("F1:G7").ClearContents
        rgData.AdvancedFilter xlFilterCopy, rgCriteria, rgOutput

        Set rgData = .Range("A8").CurrentRegion
        Set rgCriteria = .Range("D8").CurrentRegion
        Set rgOutput = .Range("F8")
    
        .Range("F8:G15").ClearContents
        rgData.AdvancedFilter xlFilterCopy, rgCriteria, rgOutput
    End With
End Sub

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM