2012-06-03 82 views
0

我對excel不是很有經驗 - 我更喜歡c#的人 - 希望一些excel的大師能夠幫助我在這裏!Excel獨特價值查詢

基本上我有一個電子表格只有一列文本數據(列a)。我需要查詢這個數據列表。

我將需要基本上將一些更多的文本數據複製到另一列(讓我們說列b),然後過濾掉列b中已存在於a列某處的記錄,只留下唯一的唯一記錄在列b中,但不是列a。

我試過使用先進的過濾器,但似乎無法得到它的工作。任何關於如何做到這一點的提示或建議都會很棒。

謝謝

回答

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 
0

這應該做你所需要的。它在A列的B列中查找每個值,並在找到匹配項時刪除該單元格。將數據粘貼到列B後運行代碼。請注意,它不會從列B中刪除重複項,它只是刪除列B中列A中的所有值。要從列B中刪除重複值,請選擇列並從Data選項卡中選擇Remove Duplicates

你需要一個模塊添加到工作簿,然後插入模塊下面的代碼:

代碼:

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

0

NON VBA METHOD

將這個公式中細胞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