Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default Select Distinct from Array

I was looking for a simple way to remove duplicates from an array; something
that was equivalent to running a SELECT DISTINCT. I didn't find exactly what
I was looking for but I found something close and modified it. Hopefully
someone else will find this useful, or perhaps even improve upon it.

Function DistinctArray(oInputArray, _
Optional MatchCase As Boolean = True, _
Optional OmitBlanks As Boolean = True)

'declare the variables
Dim oOutputArray As Variant
Dim oElement As Variant
Dim oDictionary As Dictionary

'create new dictionary object
'requires Microsoft Scripting Runtime reference
Set oDictionary = New Dictionary

'set case sensitivity
oDictionary.CompareMode = Abs(Not MatchCase)

'load elements from array into dictionary replacing duplicates
For Each oElement In oInputArray
oDictionary.Item(CStr(oElement)) = oElement
Next

'delete any blanks
If OmitBlanks Then
If oDictionary.Exists("") Then oDictionary.Remove ("")
End If

'load dictionary items into new array
oOutputArray = oDictionary.Items

'return the new array with distinct values only
DistinctArray = oOutputArray

End Function

The original function can be found he
http://home.pacbell.net/beban/
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Select Distinct from Array

You can do a similar thing with a Collection and then you don't need the
Scripting reference.
Or sort your array first, then step through the elements copy distinct
elements to a new array.

NickHK

"Lazzaroni" wrote in message
...
I was looking for a simple way to remove duplicates from an array;

something
that was equivalent to running a SELECT DISTINCT. I didn't find exactly

what
I was looking for but I found something close and modified it. Hopefully
someone else will find this useful, or perhaps even improve upon it.

Function DistinctArray(oInputArray, _
Optional MatchCase As Boolean = True, _
Optional OmitBlanks As Boolean = True)

'declare the variables
Dim oOutputArray As Variant
Dim oElement As Variant
Dim oDictionary As Dictionary

'create new dictionary object
'requires Microsoft Scripting Runtime reference
Set oDictionary = New Dictionary

'set case sensitivity
oDictionary.CompareMode = Abs(Not MatchCase)

'load elements from array into dictionary replacing duplicates
For Each oElement In oInputArray
oDictionary.Item(CStr(oElement)) = oElement
Next

'delete any blanks
If OmitBlanks Then
If oDictionary.Exists("") Then oDictionary.Remove ("")
End If

'load dictionary items into new array
oOutputArray = oDictionary.Items

'return the new array with distinct values only
DistinctArray = oOutputArray

End Function

The original function can be found he
http://home.pacbell.net/beban/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 200
Default Select Distinct from Array

NickHK wrote:
You can do a similar thing with a Collection and then you don't need the
Scripting reference.


Could you post the comparable code for a Collection approach?
Or sort your array first, then step through the elements copy distinct
elements to a new array.


Why is it necessary to sort the array first?

Alan

NickHK

"Lazzaroni" wrote in message
...
I was looking for a simple way to remove duplicates from an array;

something
that was equivalent to running a SELECT DISTINCT. I didn't find exactly

what
I was looking for but I found something close and modified it. Hopefully
someone else will find this useful, or perhaps even improve upon it.

Function DistinctArray(oInputArray, _
Optional MatchCase As Boolean = True, _
Optional OmitBlanks As Boolean = True)

'declare the variables
Dim oOutputArray As Variant
Dim oElement As Variant
Dim oDictionary As Dictionary

'create new dictionary object
'requires Microsoft Scripting Runtime reference
Set oDictionary = New Dictionary

'set case sensitivity
oDictionary.CompareMode = Abs(Not MatchCase)

'load elements from array into dictionary replacing duplicates
For Each oElement In oInputArray
oDictionary.Item(CStr(oElement)) = oElement
Next

'delete any blanks
If OmitBlanks Then
If oDictionary.Exists("") Then oDictionary.Remove ("")
End If

'load dictionary items into new array
oOutputArray = oDictionary.Items

'return the new array with distinct values only
DistinctArray = oOutputArray

End Function

The original function can be found he
http://home.pacbell.net/beban/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Select Distinct from Array

Alan,
Using a Collection, you add each array(i) value to the collection as the
Key.
Because key values must be unique, it will error if you try to add a
duplicate :

Dim arr As Variant
Dim col As Collection
Dim i As Long
Const Vals As String = "a,b,b,d,e,f,a,g,f,h,t"

'Make an array
arr = Split(Vals, ",")

'create an instance of a collection
Set col = New Collection

On Error Resume Next
For i = LBound(arr) To UBound(arr)
'This will error if arr(i) already exist in the collection
col.Add arr(i), arr(i)
Next
On Error GoTo 0

MsgBox "Started with " & i - 1 & " elements in array." & vbNewLine _
& i - 1 - col.Count & " duplicate values removed."

For the array, it is not necessary to sort, but it makes it easier.
Then you can compare arr(i) with arr(i+1) to see if it is duplicate.
Without sorting, you have to compare arr(i) to all previous values.

Personally, I prefer using a collection.

NickHK

"Alan Beban" <unavailable wrote in message
...
NickHK wrote:
You can do a similar thing with a Collection and then you don't need the
Scripting reference.


Could you post the comparable code for a Collection approach?
Or sort your array first, then step through the elements copy distinct
elements to a new array.


Why is it necessary to sort the array first?

Alan

NickHK

"Lazzaroni" wrote in message
...
I was looking for a simple way to remove duplicates from an array;

something
that was equivalent to running a SELECT DISTINCT. I didn't find exactly

what
I was looking for but I found something close and modified it.

Hopefully
someone else will find this useful, or perhaps even improve upon it.

Function DistinctArray(oInputArray, _
Optional MatchCase As Boolean = True, _
Optional OmitBlanks As Boolean = True)

'declare the variables
Dim oOutputArray As Variant
Dim oElement As Variant
Dim oDictionary As Dictionary

'create new dictionary object
'requires Microsoft Scripting Runtime reference
Set oDictionary = New Dictionary

'set case sensitivity
oDictionary.CompareMode = Abs(Not MatchCase)

'load elements from array into dictionary replacing duplicates
For Each oElement In oInputArray
oDictionary.Item(CStr(oElement)) = oElement
Next

'delete any blanks
If OmitBlanks Then
If oDictionary.Exists("") Then oDictionary.Remove ("")
End If

'load dictionary items into new array
oOutputArray = oDictionary.Items

'return the new array with distinct values only
DistinctArray = oOutputArray

End Function

The original function can be found he
http://home.pacbell.net/beban/





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 200
Default Select Distinct from Array

NickHK wrote:
Alan,
Using a Collection, you add each array(i) value to the collection as the
Key.
Because key values must be unique, it will error if you try to add a
duplicate :

Dim arr As Variant
Dim col As Collection
Dim i As Long
Const Vals As String = "a,b,b,d,e,f,a,g,f,h,t"

'Make an array
arr = Split(Vals, ",")

'create an instance of a collection
Set col = New Collection

On Error Resume Next
For i = LBound(arr) To UBound(arr)
'This will error if arr(i) already exist in the collection
col.Add arr(i), arr(i)
Next
On Error GoTo 0

MsgBox "Started with " & i - 1 & " elements in array." & vbNewLine _
& i - 1 - col.Count & " duplicate values removed."


Yes. The reason I asked is that my recollection is that Dictionaries
allow the features of matching case (or not) and omitting blanks (or
not), as the posted code does, and that Collections don't provide the
required features. I could be wrong.


For the array, it is not necessary to sort, but it makes it easier.
Then you can compare arr(i) with arr(i+1) to see if it is duplicate.
Without sorting, you have to compare arr(i) to all previous values.


I was thinking something like

Dim arr1(), arr2()
arr1 = Range("whatever")
i = 1
For Each elem in arr1
If IsError(Application.Match(elem, arr2, 0)) Then
arr2(i) = elem
i = i + 1
End if
Next

which doesn't rely on sorting.

Alan Beban

Personally, I prefer using a collection.

NickHK

"Alan Beban" <unavailable wrote in message
...
NickHK wrote:
You can do a similar thing with a Collection and then you don't need the
Scripting reference.

Could you post the comparable code for a Collection approach?
Or sort your array first, then step through the elements copy distinct
elements to a new array.

Why is it necessary to sort the array first?

Alan
NickHK

"Lazzaroni" wrote in message
...
I was looking for a simple way to remove duplicates from an array;
something
that was equivalent to running a SELECT DISTINCT. I didn't find exactly
what
I was looking for but I found something close and modified it.

Hopefully
someone else will find this useful, or perhaps even improve upon it.

Function DistinctArray(oInputArray, _
Optional MatchCase As Boolean = True, _
Optional OmitBlanks As Boolean = True)

'declare the variables
Dim oOutputArray As Variant
Dim oElement As Variant
Dim oDictionary As Dictionary

'create new dictionary object
'requires Microsoft Scripting Runtime reference
Set oDictionary = New Dictionary

'set case sensitivity
oDictionary.CompareMode = Abs(Not MatchCase)

'load elements from array into dictionary replacing duplicates
For Each oElement In oInputArray
oDictionary.Item(CStr(oElement)) = oElement
Next

'delete any blanks
If OmitBlanks Then
If oDictionary.Exists("") Then oDictionary.Remove ("")
End If

'load dictionary items into new array
oOutputArray = oDictionary.Items

'return the new array with distinct values only
DistinctArray = oOutputArray

End Function

The original function can be found he
http://home.pacbell.net/beban/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Select Distinct from Array

Alan,
For the Collection, if you made the routine a function, you could have a
CaseInsensitive argument, and if True, just LCase the keys before adding.
Omitting blanks code be included.

As for the sorting, using a non-VBA construct works fine, but without that,
I find it easier to sort first.

NickHK

"Alan Beban" <unavailable wrote in message
...
NickHK wrote:
Alan,
Using a Collection, you add each array(i) value to the collection as the
Key.
Because key values must be unique, it will error if you try to add a
duplicate :

Dim arr As Variant
Dim col As Collection
Dim i As Long
Const Vals As String = "a,b,b,d,e,f,a,g,f,h,t"

'Make an array
arr = Split(Vals, ",")

'create an instance of a collection
Set col = New Collection

On Error Resume Next
For i = LBound(arr) To UBound(arr)
'This will error if arr(i) already exist in the collection
col.Add arr(i), arr(i)
Next
On Error GoTo 0

MsgBox "Started with " & i - 1 & " elements in array." & vbNewLine _
& i - 1 - col.Count & " duplicate values removed."


Yes. The reason I asked is that my recollection is that Dictionaries
allow the features of matching case (or not) and omitting blanks (or
not), as the posted code does, and that Collections don't provide the
required features. I could be wrong.


For the array, it is not necessary to sort, but it makes it easier.
Then you can compare arr(i) with arr(i+1) to see if it is duplicate.
Without sorting, you have to compare arr(i) to all previous values.


I was thinking something like

Dim arr1(), arr2()
arr1 = Range("whatever")
i = 1
For Each elem in arr1
If IsError(Application.Match(elem, arr2, 0)) Then
arr2(i) = elem
i = i + 1
End if
Next

which doesn't rely on sorting.

Alan Beban

Personally, I prefer using a collection.

NickHK

"Alan Beban" <unavailable wrote in message
...
NickHK wrote:
You can do a similar thing with a Collection and then you don't need

the
Scripting reference.
Could you post the comparable code for a Collection approach?
Or sort your array first, then step through the elements copy distinct
elements to a new array.
Why is it necessary to sort the array first?

Alan
NickHK

"Lazzaroni" wrote in message
...
I was looking for a simple way to remove duplicates from an array;
something
that was equivalent to running a SELECT DISTINCT. I didn't find

exactly
what
I was looking for but I found something close and modified it.

Hopefully
someone else will find this useful, or perhaps even improve upon it.

Function DistinctArray(oInputArray, _
Optional MatchCase As Boolean = True, _
Optional OmitBlanks As Boolean = True)

'declare the variables
Dim oOutputArray As Variant
Dim oElement As Variant
Dim oDictionary As Dictionary

'create new dictionary object
'requires Microsoft Scripting Runtime reference
Set oDictionary = New Dictionary

'set case sensitivity
oDictionary.CompareMode = Abs(Not MatchCase)

'load elements from array into dictionary replacing duplicates
For Each oElement In oInputArray
oDictionary.Item(CStr(oElement)) = oElement
Next

'delete any blanks
If OmitBlanks Then
If oDictionary.Exists("") Then oDictionary.Remove ("")
End If

'load dictionary items into new array
oOutputArray = oDictionary.Items

'return the new array with distinct values only
DistinctArray = oOutputArray

End Function

The original function can be found he
http://home.pacbell.net/beban/





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Distinct Maximums Zeepowlee Excel Worksheet Functions 3 April 5th 06 06:16 PM
howto select distinct values from list chris Excel Worksheet Functions 6 April 17th 05 12:25 PM
Select Distinct Items in a Column Raul Excel Programming 3 December 3rd 04 05:48 PM
Using an array to select data Tony Barla Excel Programming 3 October 1st 04 05:00 PM
select distinct row weejeow Excel Programming 8 April 29th 04 12:21 PM


All times are GMT +1. The time now is 12:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"