2014-10-30 109 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 
相關問題