WSanders2015
New Member
- Joined
- Nov 18, 2015
- Messages
- 10
I'm re-engineering a subroutine to remove the duplicate rows from a listbox; the "ColumnCount" property of the listbox is set to "13". If I don't call my duplicate removal subroutine, the listbox correctly contains all of the columns of data; however, several rows are dupliated. The subroutine is listed below:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">The problem begins with the following line of code:
It's only adding the first column of my 13-column worksheet to the collection variable "nodupes". I would like to add an entire row from the worksheet to the sheet. How do I modify my collection to accept a complete row of data, not just the first cell of a row, such that listbox is properly reconstructed when the following code is executed?
</code>
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">[COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] RemoveDuplicateListBoxRows[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Long[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] j [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Long[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] nodupes [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]New[/COLOR][COLOR=#000000] Collection
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] Swap1[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Swap2[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Item
[/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#00008B]Me[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]lbSrchMatchingResults
[/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#00008B]To[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ListCount [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]' The next statement ignores the error caused[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]' by attempting to add a duplicate key to the collection.[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]' The duplicate is not added - which is just what we want![/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]On[/COLOR][COLOR=#00008B]Error[/COLOR][COLOR=#00008B]Resume[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000]
nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]List[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]),[/COLOR][COLOR=#00008B]CStr[/COLOR][COLOR=#000000](.[/COLOR][COLOR=#000000]List[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000] i
[/COLOR][COLOR=#808080]' Resume normal error handling[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]On[/COLOR][COLOR=#00008B]Error[/COLOR][COLOR=#00008B]GoTo[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'Clear the listbox[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Clear
[/COLOR][COLOR=#808080]'Sort the collection (optional)[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#00008B]To[/COLOR][COLOR=#000000] nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Count [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#000000] j [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#00008B]To[/COLOR][COLOR=#000000] nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Count
[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000] nodupes[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000] nodupes[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]j[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
Swap1 [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] nodupes[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
Swap2 [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] nodupes[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]j[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add Swap1[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] before[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#000000]j
nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add Swap2[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] before[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#000000]i
nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Remove i [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]
nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Remove j [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000] j
[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000] i
[/COLOR][COLOR=#808080]' Add the sorted and non-duplicated items to the ListBox[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#00008B]Each[/COLOR][COLOR=#000000] Item [/COLOR][COLOR=#00008B]In[/COLOR][COLOR=#000000] nodupes
[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AddItem Item
[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000] Item
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Sub[/COLOR]</code>
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">The problem begins with the following line of code:
Code:
[COLOR=#00008B]<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">[COLOR=#000000]nodupes[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]List[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]),[/COLOR][COLOR=#00008B]CStr[/COLOR][COLOR=#000000](.[/COLOR][COLOR=#000000]List[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]))
[/COLOR]</code>[/COLOR]
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">[COLOR=#00008B]For[/COLOR][COLOR=#00008B]Each[/COLOR][COLOR=#000000] Item [/COLOR][COLOR=#00008B]In[/COLOR][COLOR=#000000] nodupes
[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AddItem Item
[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000] Item[/COLOR]</code>