将数组分配给.RemoveDuplicates列:=

时间:2015-03-03 14:40:56

标签: excel vba

我想删除包含数据的列范围内的重复行。

我首先获得数据集

使用的最后一行和最后一列
lastUsedRowDiff = resultBook.Sheets("Differences").Cells(resultBook.Sheets("Differences").Rows.Count, "A").End(xlUp).Row
lastUsedColumnDiff = resultBook.Sheets("Differences").Cells(6, resultBook.Sheets("Differences").Columns.Count).End(xlToLeft).Column

我尝试使用RemoveDuplicate函数,如下所示:

resultBook.Sheets("Differences").range(resultBook.Sheets("Differences").Cells(1, 1), resultBook.Sheets("Differences").Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44), _
Header:=xlNo

它有效,但我希望列数是动态的,因此初始化保存列的数组是不切实际的。

我尝试在调用RemoveDuplicates之前初始化一个包含列索引的数组,如下所示:

ReDim columnArray(1 To lastUsedColumnDiff) As Integer
For p = 1 To lastUsedColumnDiff
    columnArray(p) = p
Next p

当我尝试将columnArray分配给Columns:=我收到错误。

resultBook.Sheets("Differences").range(resultBook.Sheets("Differences").Cells(1, 1), resultBook.Sheets("Differences").Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates Columns:=columnArray, _
Header:=xlNo

以下是整个代码段:

lastUsedRowDiff = resultBook.Sheets("Differences").Cells(resultBook.Sheets("Differences").Rows.Count, "A").End(xlUp).Row
lastUsedColumnDiff = resultBook.Sheets("Differences").Cells(6, resultBook.Sheets("Differences").Columns.Count).End(xlToLeft).Column

ReDim columnArray(1 To lastUsedColumnDiff) As Integer
For p = 1 To lastUsedColumnDiff
    columnArray(p) = p
Next p

resultBook.Sheets("Differences").range(resultBook.Sheets("Differences").Cells(1, 1), resultBook.Sheets("Differences").Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates Columns:=columnArray, _
Header:=xlNo

4 个答案:

答案 0 :(得分:3)

好的,这很有趣......一眼就看出来,我不明白为什么它不会在Columns方法调用中接受RemoveDuplicates的数组参数。 Google提出this回答,建议在阵列上使用VBA Evaluate函数,当我测试它时,它似乎按预期工作。

  

麻烦似乎是RemoveDuplicates方法似乎认为   cols是某种功能。尝试使用evaluate()方法将其识别为变量。这在Excel 2007中对我有用,应该适合你。让我知道。

您的代码也可以从一些清理中受益,试试这个,它使用With块来使代码更容易阅读&修改:

Sub foo()
Dim lastUsedRowDiff As Long
Dim lastUsedColumnDiff As Long
Dim myWorkbook As Workbook
Dim mySheet As Worksheet
Dim columnArray()
Dim p As Long

Set myWorkbook = ThisWorkbook       '## Modify as needed
Set mySheet = ThisWorkbook.Sheets(1) '## Modify as needed

With mySheet
    lastUsedRowDiff = .Cells(.Rows.Count, "A").End(xlUp).Row
    lastUsedColumnDiff = .Cells(6, .Columns.Count).End(xlToLeft).Column
End With

ReDim columnArray(1 To lastUsedColumnDiff)
For p = 1 To lastUsedColumnDiff
    columnArray(p) = p
Next

With mySheet
    .Range(.Cells(1, 1), .Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates _
        Columns:=Evaluate(columnArray), Header:=xlNo
End With

End Sub

答案 1 :(得分:2)

正如我刚刚发现的,Columns:= Evaluate(columnArray)方法可能无法正确处理。删除了太多行,不确定原因。尝试Columns:=(columnArray)。这在Excel 2010中对我有用。

答案 2 :(得分:1)

另外 - 使用Columns:=(columnArray)确保columnArray从0开始索引。否则,您将得到运行时错误“9”:下标超出范围。 所以这应该如下所示:

Sub RemoveDuplicates()

Dim columns As Long
Dim mySheet As Worksheet
Dim myArray()

Set mySheet = ThisWorkbook.Sheets(1) 'To modify

columns = mySheet.UsedRange.columns.Count

ReDim myArray(0 To columns - 1)

For i = 0 To columns - 1
    myArray(i) = i + 1
Next i

mySheet.UsedRange.RemoveDuplicates columns:=(myArray), Header:=xlYes

End Sub

在MS Excel 2013上测试。

答案 3 :(得分:0)

这是另一种解决方案

Private Sub removeDuplicates(ByRef arrName As Variant)
  Dim list As New Dictionary
  Dim item As Variant

  For Each item In arrName

    If Not list.Exists(item) Then
        list.Add item, item
    End If
  Next

  arrName = list.Items
End Sub

然后重新分配到范围。如果要仅在vba中从数组中删除重复项,也可以使用它。