OK so this is interesting... At a glance, I don't understand why it won't accept an array argument for Columns
in the RemoveDuplicates
method call. Google turned up this answer, which suggests using the VBA Evaluate
function on the array, and when I test it, it seems to work as expected.
The trouble seems to be that the RemoveDuplicates method seems to think that
cols is a function of some sort. Try using the evaluate() method to get it to recognize it as a variable. This worked for me in Excel 2007 and should work for you. Let me know.
Your code could also benefit from some cleaning up, try this, which uses With
blocks to make the code easier to read & modify:
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