如果您需要 VBA 解决方案来删除唯一的 A:D 值,请尝试以下代码:
Sub UniqueRows()
'it needs a reference to `Mirosoft Scripting Runtime`
Dim sh As Worksheet, lastR As Long, arr, arrFin, arrEl, i As Long
Dim dict As New Scripting.Dictionary
Set sh = ActiveSheet ' use here your necessary sheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).row 'last row in A:A
arr = sh.Range("A2:D" & lastR).Value 'Put the range in an array
ReDim arrFin(1 To 2, 1 To UBound(arr)) 'redim the final array
For i = 1 To UBound(arr)
If Not dict.Exists(arr(i, 1) & "," & arr(i, 4)) Then
dict.Add arr(i, 1) & "," & arr(i, 4), 1 'keep only the unique cases
End If
Next i
'build an array from the dictionary keys:
For i = 0 To dict.count - 1
arrEl = Split(dict.Keys(i), ",")
arrFin(1, i + 1) = arrEl(0): arrFin(2, i + 1) = arrEl(1)
Next i
ReDim Preserve arrFin(1 To 2, 1 To i) 'redim the array to the exact number of real elements
'drop the array content in the range you need:
sh.Range("F2").Resize(UBound(arrFin, 2), 2).Value = Application.Transpose(arrFin)
End Sub
代码将在同一张表中返回处理后的结果,从“F2”单元格开始。代码可以很容易地适应返回到任何你需要的地方。
如果不熟悉添加引用,请在运行上面的代码之前运行下一个代码。它会自动添加必要的引用:
Sub addScrRunTimeRef()
'Add a reference to 'Microsoft Scripting Runtime':
'In case of error ('Programmatic access to Visual Basic Project not trusted'):
'Options->Trust Center->Trust Center Settings->Macro Settings->Developer Macro Settings->
' check "Trust access to the VBA project object model"
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\SysWOW64\scrrun.dll"
End Sub
这项工作可以在没有参考的情况下完成,但我认为最好拥有它以便从 Intellisense 建议中受益。