2014-10-30 124 views
3

我有一段代码,通过查看两列(第3列& 5)从表中删除重复项。Excel VBA - RemoveDuplicates方法不适用于Mac

lRow = .Cells(Rows.Count, "A").End(xlUp).Row 
'.Range("A1:BR" & lRow).RemoveDuplicates Columns:=Array(3, 5), Header:=xlYes 
.Range("$A$1:$BR$" & lRow).RemoveDuplicates Columns:=Array(3, 5), Header:=xlYes 

它在Windows中正常工作,但不幸的是没有在Mac上。

任何人都可以请建议我在这里需要更改什么?

+2

据我记得'.RemoveDuplicates'在Excel 2011中不起作用。您将不得不遍历范围以查找重复项。 – 2014-10-30 14:31:15

+1

根据@ SiddharthRout的建议,使用字典或收集方法。这更复杂,但它可以很好地完成工作。 – Manhattan 2014-10-30 15:49:19

+0

@SiddharthRout&Nanashi:好的,谢谢。我会写一个自定义代码,然后:( – Tejas 2014-10-30 17:29:24

回答

0

这段代码将创建一个唯一值列表并复制到另一个单元格中。所以创建独特的列表。

您必须指定列表的起始位置以及要复制到的位置。您可以通过更改fromCell和toCell变量来完成此操作。我希望这有帮助。

Sub uniqueList() 

    fromCell = "A1" 
    toCell = "B1" 

    fromColumn = Mid(fromCell, 1, 1) 'This will resolve to A 
    toColumn = Mid(toCell, 1, 1)  'This will resolve to B 

    fromRow = Mid(fromCell, 2)  'This will resolve to 1 
    toRow = Mid(toCell, 2)   'This will resolve to 1 


    Dim cl As Range, UniqueValues As New Collection, uValue As Variant 
    Application.Volatile 

    numRows = Range(fromCell).End(xlDown).Row 

    On Error Resume Next 
    For Each cl In Range(fromCell & ":" & fromColumn & numRows) 
     UniqueValues.Add cl.Value, CStr(cl.Value) 
    Next cl 

    y = toRow - 1 

    For Each uValue In UniqueValues 
     y = y + 1 
     Range(toColumn & y) = uValue 
    Next uValue 


End Sub 
相关问题