Excel唯一值查询

时间:2012-06-03 13:59:54

标签: excel excel-vba vba

我对excel不是很有经验 - 我更像是一个c#家伙 - 希望一些优秀的大师可以帮助我在这里!

基本上我有一个只有一列文本数据的电子表格(列a)。我需要查询这个数据列表。

我需要基本上将一些更多的文本数据复制到另一列(比如列b),然后过滤出b列中已经存在于a列某处的记录,只留下唯一的记录在列b中,但不在列a中。

我尝试使用高级过滤器但似乎无法使其正常工作。关于如何做到这一点的任何提示或建议都会很棒。

由于

3 个答案:

答案 0 :(得分:1)

您可以动态过滤数据,例如使用

等公式进入C列
=IF(ISNA(VLOOKUP(B1,A:A,1,FALSE)),B1,"")

然后过滤C列中的非空单元格

否则这个简单的宏将清除重复的地方

Sub FilterDuplicates()
  Dim r As Range
  For Each r In ActiveSheet.Columns("B").Cells
    If r.Value <> "" Then
      On Error Resume Next
      WorksheetFunction.VLookup r, ActiveSheet.Columns("A"), 1, False
      If Err.Number = 0 Then r.ClearContents
      On Error GoTo 0
    End If
  Next r
End Sub

答案 1 :(得分:0)

这应该做你需要的。它在A列中查找B列中的每个值,如果找到匹配则删除该单元格。将数据粘贴到B列后运行代码。请注意,它不会从B列中删除重复项,只删除B列中列A中的任何值。要从列B中删除dupes,请选择列然后从Remove Duplicates标签中选择Data

您需要将一个模块添加到工作簿中,并在模块中插入以下代码:

<强>码

Option Explicit

Sub RemoveMatchesFromColumn()
    On Error Resume Next

    Dim LastRow As Long
    Dim SearchText As String
    Dim MatchFound As String

    LastRow = Range("b" & ActiveSheet.Rows.Count).End(xlUp).Row
    SearchText = Range("b" & LastRow).Value

    Do Until LastRow = 0

    MatchFound = Find_Range(SearchText, Columns("A")).Value
        If SearchText = MatchFound Then
            Range("b" & LastRow).Delete Shift:=xlUp
        End If
        LastRow = LastRow - 1
        SearchText = Range("b" & LastRow).Value
    Loop

End Sub


Function Find_Range(Find_Item As Variant, _
    Search_Range As Range, _
    Optional LookIn As Variant, _
    Optional LookAt As Variant, _
    Optional MatchCase As Boolean) As Range

     ' Function written by Aaron Blood
     ' http://www.ozgrid.com/forum/showthread.php?t=27240

    Dim c As Range
    Dim firstAddress As Variant
    If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
    If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
    If IsMissing(MatchCase) Then MatchCase = False

    With Search_Range
        Set c = .Find( _
        What:=Find_Item, _
        LookIn:=LookIn, _
        LookAt:=LookAt, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=MatchCase, _
        SearchFormat:=False)
        If Not c Is Nothing Then
            Set Find_Range = c
            firstAddress = c.Address
            Do
                Set Find_Range = Union(Find_Range, c)
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With

End Function

运行子RemoveMatchesFromColumn。你可以进入它看看它正在做什么 F8 或者用 F5 运行它。

答案 2 :(得分:0)

非VBA方法

将此公式放入Cell C1

=IF(VLOOKUP(B1,A:A,1,0)=B1,"DELETE ME","")

拖动它直到结束。然后在Col C上过滤DELETE ME的数据,然后删除重复的数据。

VBA方法

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long
    Dim delRange As Range, aCell As Range

    Set ws = Sheets("Sheet1")
    With ws
        lRow = .Range("B" & Rows.Count).End(xlUp).Row
        For i = 1 To lRow
            Set aCell = .Columns(1).Find(What:=.Range("B" & i).Value, _
                        LookIn:=xlValues, LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False)

            If Not aCell Is Nothing Then
                If delRange Is Nothing Then
                    Set delRange = .Range("B" & i)
                Else
                    Set delRange = Union(delRange, .Range("B" & i))
                End If
            End If
        Next i
        If Not delRange Is Nothing Then delRange.Delete shift:=xlUp
    End With
End Sub