简体   繁体   English

基于另一个工作簿中的单元格值自动筛选

[英]Autofilter based on a cell values in another workbook

I am trying to filter a table that is located on a different workbook.我正在尝试过滤位于不同工作簿上的表。

Scenario: I have 2 workbooks.场景:我有 2 个工作簿。 WB1 has several drop down menus to filter the data that is on WB2. WB1 有几个下拉菜单来过滤 WB2 上的数据。

I want to filter and copy the data from WB2 based on the multiple criterions on WB1 and paste it on an existing tab on WB1 as well我想根据 WB1 上的多个标准过滤和复制 WB2 中的数据,并将其粘贴到 WB1 上的现有选项卡上

I have been working on this for about 8 or 9 hours now with no success.我已经为此工作了大约 8 或 9 个小时,但没有成功。

So far this is what I have:到目前为止,这就是我所拥有的:

Sub filter_by_cell_value()

    Dim WB1 As Workbook
    Dim WB2 As Workbook
    
    Set WB1 = ActiveWorkbook
    
    Workboooks.Open Filename = "C:\Users\name\Documents\jbl\Extract.xlsb"
    'Capture new workbook
    Set WB2 = ActiveWorkbook
    
    With ActiveWorkbook.Sheets("AU06").AutoFilterMode = False
        'Workbook("Extract.xlsb").Sheets ("AU06")
    .AutoFilter Field = 2, Criteria1:="=" & ThisWorkbook.Sheets("Appendix 2").Cells(2, 9).Value
    .SpecialCells(xlCellTypeVisible).Copy
    End With
    
    WB1.Activate
    
    ThisWorkbook.Sheets("AU06").Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    Range("A1").CurrentRegion.Columns.AutoFit

End Sub

Any help at this point will be greatly appreciated.在这一点上的任何帮助将不胜感激。

I can guess, not having seen your workbooks, that the code might look something like this:我可以猜测,没有看过您的工作簿,代码可能如下所示:

Sub filter_by_cell_value()
    Dim WS1 As Worksheet, WS2 As Worksheet
    
    Set WS1 = ThisWorkbook.Sheets("AU06")
    Set WS2 = Workbooks.Open("C:\Users\name\Documents\jbl\Extract.xlsb").Sheets("AU06")
    
    With WS2
        .AutoFilterMode = False
        With .Range("A1").CurrentRegion ' your own range
            .AutoFilter Field:=2, Criteria1:=WS1.Parent.Sheets("Appendix 2").Cells(2, 9).Text
            .SpecialCells(xlCellTypeVisible).Copy
        End With
    End With
    
    With WS1.Range("A1")
        .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        .CurrentRegion.Columns.AutoFit
    End With
End Sub

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

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