【问题标题】:Name Range and then autofilter命名范围,然后自动过滤
【发布时间】:2018-04-04 00:39:38
【问题描述】:

我有两张床单。 Sheet1 (PasteHere) 在 col B 中有很长的值列表。例如:

100000
100100
100800
100801
200501
etc

Sheet2 (Landing) 有一个我需要过滤的列表。例如:

100000
100801

最终结果是我希望工作表 1 中的值被工作表 2 中的值过滤。我想我可以命名工作表 2 中的范围,然后按它过滤,但它不起作用。这是我到目前为止的代码。我将范围命名为“CustList”

Sub FilterList()

Sheets("Landing").Select
Dim LastRow1 As Long
LastRow1 = Range("B" & Rows.Count).End(xlUp).Row

Range("B15:B" & LastRow1).Select
ActiveWorkbook.Names.Add Name:="CustList", RefersToR1C1:= _
    "=Landing!R15C2:R[" & LastRow1 & "]C2"
Range("E16").Select

Dim vCrit As Variant
Dim rngCrit As Range
Set rngOrders = Sheets("PasteHere").Range("$A$1").CurrentRegion
Set rngCrit = Sheets("Landing").Range("CustList")

vCrit = rngCrit.Value

Sheets("PasteHere").Select
rngOrders.AutoFilter _
Field:=2, _
Criteria1:=Application.Transpose(vCrit), _
Operator:=xlFilterValues

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    使用下面的代码。

    Dim LastRow1, LastRow2, iLoop
    
    Sheets("Landing").Select
    
    LastRow1 = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
    
    
    
    ReDim xarr(LastRow1 - 14)
    
    
    For iLoop = 1 To LastRow1 - 14
        xarr(iLoop) = ActiveSheet.Range("B" & iLoop)
    Next
    
    Sheets("PasteHere").Select
    LastRow2 = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
    ActiveSheet.Range("B1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$B$1:$B$" & LastRow2).AutoFilter Field:=1, Criteria1:=xarr, Operator:=xlFilterValues
    

    【讨论】:

    • 很遗憾,这不起作用。它和我的代码做同样的事情。它只是过滤掉所有内容。还有其他想法吗?
    【解决方案2】:

    试试这个代码:

    Option Explicit
    
    Sub FilterRange()
    'declaration of variables
    Dim filterBy As Variant, toFilter As Variant, lastRow1 As Long, lastRow2 As Long, i As Long, j As Long, k As Long, _
    filtered As Variant, ws1 As Worksheet, ws2 As Worksheet, flag As Boolean
    k = 1
    flag = True
    'set references to worksheets, it's good to use them when you deal with more than one worksheet
    'REMEMBER: use your own sheet name and change ranges I used (I used A column)
    Set ws1 = Worksheets("Arkusz1")
    Set ws2 = Worksheets("Arkusz2")
    'set the ranges (storethem as arrays): to filter and one to filter by
    lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    toFilter = ws1.Range("A1:A" & lastRow1).Value2
    'clear range, we will write here filtered values
    ws1.Range("A1:A" & lastRow1).Clear
    lastRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
    filterBy = ws2.Range("A1:A" & lastRow1).Value2
    
    'here you loop thorugh arrays, checking if one element is in the other array
    'if it isn't, write this value to cell on ws1
    For i = 1 To lastRow1
        flag = True
        For j = 1 To lastRow2
            If toFilter(i, 1) = filterBy(j, 1) Then
                flag = False
                Exit For
            End If
        Next
    
        If flag Then
            ws1.Cells(k, 1).Value = toFilter(i, 1)
            k = k + 1
        End If
    Next
    
    End Sub
    

    【讨论】:

    • 抱歉,这也没用。我发现了一个 hack,它并不是真正解决问题的方法,而是一种解决方法。如果我在列中进行 vlookup,所有不匹配的值都将返回错误。然后我清理错误并过滤掉空白。
    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多