我对 excel 的经验不是很丰富——我更像是 ac# 的人——希望一些 excel 大师可以在这里帮助我!
基本上我有一个电子表格,它只有一列文本数据(a 列)。我需要查询这个数据列表。
我将需要基本上将更多文本数据复制到另一列(假设是 b 列),然后过滤掉 b 列中已经存在于 a 列某处的记录,只留下其中的唯一记录b列,但不是a列。
我试过使用高级过滤器,但似乎无法让它工作。关于我如何做到这一点的任何提示或建议都会很棒。
谢谢
您可以动态过滤您的数据,例如使用如下公式进入 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
这应该做你需要的。它在 A 列的 B 列中查找每个值,如果找到匹配项,则删除该单元格。将数据粘贴到 B 列后运行代码。请注意,它不会从 B 列中删除重复项,它只会从 B 列中删除 A 列中的所有值。要从 B 列中删除重复项,请选择该列并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.
非 VBA 方法
将此公式放入单元格 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