2015-10-14 47 views
1

我在Excel工作表滤波使用.Autofilter数据如下Autofiler为或病症VBA

if bProds = True then 
    .AutoFilter 4,"<>*strip*",2,"<>*Strip*" 
    .AutoFilter 8,"Product",2,"product" 
else 
    .AutoFilter 8, "<>Product", 1, "<>product" 
    .AutoFilter 4,"*strip*",2,"*Strip*" 
end if 

结果集是行,其中两个自动筛选条件满足。 在else部分,我想做一个条件,使得结果是不包含第8列中的产品/产品的行,或者它包含第4列中的strip/strip。

如果两个条件都是自动过滤器遇到,上面的代码不起作用。任何方式使它像我的要求一样工作?

试验数据也可从this drive.google.com link

表1:

Col1  Col2 Col3 Col4 Col5 Col6  Col7  Col8 
--------- ---- ---- ----- ----- ----  ----  ----- 
1/07/2015 3  3 Word1 word2 AB  Hello Product 
1/13/2015 2  1 word2 word1 AB  hello product 
1/13/2015 2  1 COzier USA  Bill  Allice Assembly 
1/14/2015 3  4 TOny USA  Bill  Allice Wrox 
1/20/2015 2  1 Shawn USA  gerard amy  Product 
1/23/2015 2  1 Wilcox Sanzer Francis Bob  Assembly 
1/26/2015 3  5 Justin Langer Go  febrand Citrix 

表2:

Col1  Col2 Col3 Col4 Col5 Col6  Col7  Col8 
--------- ---- ---- ----- ----- ----  ----  ----- 
1/20/2015 2  1 Shawn USA  gerard amy  Product 

表3:

Col1  Col2 Col3 Col4 Col5 Col6  Col7  Col8 
--------- ---- ---- ----- ----- ----  ----  ----- 
1/07/2015 3  3 Word1 word2 AB  Hello Product 
1/13/2015 2  1 word2 word1 AB  hello product 
1/13/2015 2  1 COzier USA  Bill  Allice Assembly 
1/14/2015 3  4 TOny USA  Bill  Allice Wrox 
1/23/2015 2  1 Wilcox Sanzer Francis Bob  Assembly 
1/26/2015 3  5 Justin Langer Go  febrand Citrix 
+0

我不认为,除非你使用一个辅助列,您可以用自动筛选实现这一目标。 [高级过滤](https://support.office.com/en-us/article/Filter-by-using-advanced-criteria-4c9222fe-8529-4cd7-a898-3f16abdff32b#bmexample3)可以实现这一点,如果这是可以接受的解决方案 – barrowc

+1

你能提供最少的前后测试数据吗? –

+0

我可以使用任何构造。由于现有代码正在使用.Autofilter,我只是检查是否有可能使用.Autofilter。使用.AdvanceFilter无问题,只要它达到我想要的.. :)。我做了一个测试文件'https://drive.google.com/open?id = 1fvlZnQ12vodFFqjAHaODXcF0CYFkahBKGUBE7EMt8Uc' – Programmerzzz

回答

0
Public Sub autoFilter1() 'Column 8 = "Product" 
    Dim ws As Worksheet 

    Set ws = Worksheets("Sheet1") 
    ws.UsedRange.Rows.EntireRow.Hidden = False 
    If Not ws.AutoFilter Is Nothing Then ws.ShowAllData 

    ws.UsedRange.AutoFilter 8, "product" 
End Sub 

Public Sub autoFilter2() 'Column 8 <> "Product" OR Column 4 = "Word1" 
    Dim ws As Worksheet, vRng As Range 

    Application.ScreenUpdating = False 
    Set ws = Worksheets("Sheet1") 
    ws.UsedRange.Rows.EntireRow.Hidden = False 
    If Not ws.AutoFilter Is Nothing Then ws.ShowAllData 

    ws.UsedRange.AutoFilter 8, "<>product" 
    Set vRng = ws.UsedRange.SpecialCells(xlCellTypeVisible) 
     ws.ShowAllData 
    ws.UsedRange.AutoFilter 4, "word1" 
    Set vRng = Union(vRng, ws.UsedRange.SpecialCells(xlCellTypeVisible)) 
    ws.UsedRange.AutoFilter 

    ws.UsedRange.Rows.EntireRow.Hidden = True 
    vRng.Rows.EntireRow.Hidden = False 
    Application.ScreenUpdating = True 
    ws.Cells(1).Select 
End Sub 

multiAutoFilter

+0

谢谢,让我测试并找回 – Programmerzzz

+0

这不完全是我想要的。可能是我的问题陈述并不清楚,正如你所说的。例如: - 在我的数据集中,我们根据第8列和第4列进行过滤。我需要将包含第8列中的“产品”的记录分离出来,在第8列中将非产品值记录到工作表B中。除此之外,在工作表BI中,需要在第4列中具有“word1”的记录,虽然他们在第8列中有“产品”。原始文章中的.Autofilter条件符合我的其他条件的逻辑“AND”。但是我需要像我提到的那样的'OR'行为。希望我清楚 – Programmerzzz

+0

对于预期结果(可能是因为无效的测试数据),我仍然不清楚:您在Sheet B_的第8列中表示_non产品值,但您的Sheet B仅包含一个记录,即“产品” 。我更新了您的初始文章,以包含您在外部文件中提供的示例数据,但我需要进行基本设置:1)记录的初始列表2)预期结果 - 基于2个条件的2个结果,来自初始记录集 –