pbornemeier
Well-known Member
- Joined
- May 24, 2005
- Messages
- 3,915
Working to answer this thread:
https://www.mrexcel.com/forum/excel...ions-multiple-columns-without-duplicates.html
I needed a way to remove duplicates from a variable number of columns
VBA recorded for remove duplicates in H:J of columns A:J is:
So worked up this code which I thought would work:
This code built an array that looked like this: Array(8, 9, 10), but it would not work and returned Run-time error '5': Invalid procedure call or argument
aryDeDupe matched the array structure in the recorded code and in the Locals window. I did not understand why it would not work.
I searched and found the solution in post #11 of https://www.mrexcel.com/forum/excel-questions/470729-removing-duplicates-dynamic-columns-vba-2.html
which when applied to the last line of my code added parenthesis around aryDeDupe:
This worked, I don't understand why or how it might be applied to other Excel VBA code. An explanation or a link to same would be appreciated.
I added a post to the second thread that asked the same question I posed here, but thought it more likely would get a response if I asked directly.
https://www.mrexcel.com/forum/excel...ions-multiple-columns-without-duplicates.html
I needed a way to remove duplicates from a variable number of columns
VBA recorded for remove duplicates in H:J of columns A:J is:
Code:
ActiveSheet.Range("$A$1:$J$29").RemoveDuplicates Columns:=Array(8, 9, 10), Header:=xlYes
So worked up this code which I thought would work:
Code:
'Check for duplicate rows in HSort Columns
' Can only happen if names are duplicated within an input column
' Build aryDeDupe -- Array(1, 2, 3,...n) -- to exclude iteration # column
lLastRow = Cells(Rows.Count, lFirstWriteColumn).End(xlUp).Row
ReDim aryDeDupe(0 To lLastHSortColumn - lFirstHSortColumn)
lIndex = 0
For lColumnIndex = lFirstHSortColumn To lLastHSortColumn
aryDeDupe(lIndex) = CInt(lColumnIndex - lFirstWriteColumn + 1)
lIndex = lIndex + 1
Next
ActiveSheet.Cells(1, lFirstWriteColumn).CurrentRegion.RemoveDuplicates Columns:=[COLOR="#0000FF"]aryDeDupe[/COLOR], Header:=xlYes
This code built an array that looked like this: Array(8, 9, 10), but it would not work and returned Run-time error '5': Invalid procedure call or argument
aryDeDupe matched the array structure in the recorded code and in the Locals window. I did not understand why it would not work.
I searched and found the solution in post #11 of https://www.mrexcel.com/forum/excel-questions/470729-removing-duplicates-dynamic-columns-vba-2.html
which when applied to the last line of my code added parenthesis around aryDeDupe:
Code:
ActiveSheet.Cells(1, lFirstWriteColumn).CurrentRegion.RemoveDuplicates Columns:=[COLOR="#FF0000"]([/COLOR][COLOR="#0000FF"]aryDeDupe[/COLOR][COLOR="#FF0000"])[/COLOR], Header:=xlYes
This worked, I don't understand why or how it might be applied to other Excel VBA code. An explanation or a link to same would be appreciated.
I added a post to the second thread that asked the same question I posed here, but thought it more likely would get a response if I asked directly.