Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 3.50 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Count number of different values in chosen (large) range in VBA?
  2. Sub Samples()
  3.     Dim scol As New Collection
  4.  
  5.     With Sheets("Sheet1")
  6.         For i = 1 To 100 '<~~ Assuming the range is from A1 to A100
  7.             On Error Resume Next
  8.             scol.Add .Range("A" & i).Value, Chr(34) & _
  9.             .Range("A" & i).Value & Chr(34)
  10.             On Error GoTo 0
  11.         Next i
  12.     End With
  13.  
  14.     Debug.Print scol.Count
  15.  
  16.     'For Each itm In scol
  17.     '   Debug.Print itm
  18.     'Next
  19. End Sub
  20.        
  21. Sub Samples()
  22.     Dim scol As New Collection
  23.     Dim MyAr As Variant
  24.  
  25.     With Sheets("Sheet1")
  26.         '~~> Select your range in a column here
  27.         MyAr = .Range("A1:A10").Value
  28.  
  29.         For i = 1 To UBound(MyAr)
  30.             On Error Resume Next
  31.             scol.Add MyAr(i, 1), Chr(34) & _
  32.             MyAr(i, 1) & Chr(34)
  33.             On Error GoTo 0
  34.         Next i
  35.     End With
  36.  
  37.     Debug.Print scol.Count
  38.  
  39.     'For Each itm In scol
  40.     '   Debug.Print itm
  41.     'Next
  42. End Sub
  43.        
  44. Function UniqueEntryCount(SourceRange As Range) As Long
  45.  
  46.     Dim MyDataset As Variant
  47.     Dim dic As Scripting.Dictionary
  48.     Set dic = New Scripting.Dictionary
  49.  
  50.     MyDataset = SourceRange
  51.  
  52.     On Error Resume Next
  53.  
  54.     Dim i As Long
  55.  
  56.     For i = 1 To UBound(MyDataset, 1)
  57.  
  58.         dic.Add MyDataset(i, 1), ""
  59.  
  60.     Next i
  61.  
  62.     On Error GoTo 0
  63.  
  64.     UniqueEntryCount = dic.Count
  65.  
  66.     Set dic = Nothing
  67.  
  68. End Function
  69.        
  70. Function UniqueEntryCount(SourceRange As Range) As Long
  71.  
  72.     Dim MyDataset As Variant
  73.     Dim dic As Scripting.Dictionary
  74.     Set dic = New Scripting.Dictionary
  75.  
  76.     MyDataset = SourceRange
  77.  
  78.     Dim i As Long
  79.  
  80.     For i = 1 To UBound(MyDataset, 1)
  81.  
  82.         if not dic.Exists(MyDataset(i,1)) then dic.Add MyDataset(i, 1), ""
  83.  
  84.     Next i
  85.  
  86.     UniqueEntryCount = dic.Count
  87.  
  88.     Set dic = Nothing
  89.  
  90. End Function
  91.        
  92. Dim MyDataset As Variant
  93.     Dim MyRow As Variant
  94.     Dim MyCell As Variant
  95.     Dim dic As Object
  96.     Dim l1 As Long, l2 As Long
  97.  
  98.     Set dic = CreateObject("Scripting.Dictionary")
  99.     MyDataset = SourceRange
  100.  
  101.     For l1 = 1 To UBound(MyDataset)
  102.         ' There is no function to get the UBound of the 2nd dimension
  103.         ' of an array (that I'm aware of), so use this division to
  104.         ' get this value. This does not work for >=3 dimensions!
  105.         For l2 = 1 To SourceRange.Count / UBound(MyDataset)
  106.             If Not dic.Exists(MyDataset(l1, l2)) Then
  107.                 dic.Add MyDataset(l1, l2), MyDataset(l1, l2)
  108.             End If
  109.         Next l2
  110.     Next l1
  111.  
  112.     UniqueEntryCount = dic.Count
  113.     Set dic = Nothing
  114.  
  115. End Function
  116.        
  117. For l1 = 1 To UBound(MyDataset)
  118.         For l2 = 1 To SourceRange.Count / UBound(MyDataset)
  119.             If Not dic.Exists(MyDataset(l1, l2)) And MyDataset(l1, l2) <> "" Then
  120.                 dic.Add MyDataset(l1, l2), MyDataset(l1, l2)
  121.             End If
  122.         Next l2
  123.     Next l1
  124.        
  125. // first copy the array so you don't lose any data
  126. List<value> copiedList = new List<value>(yourArray.ToList());
  127.  
  128. //for through your list so you test every value
  129. for (int a = 0; a < copiedList.Count; a++)
  130. {
  131.   // copy instances to a new list so you can count the values and do something with them
  132.   List<value> subList = new List<value>(copiedList.FindAll(v => v == copiedList[i]);
  133.  
  134.   // do not do anything if there is only 1 value found
  135.   if(subList.Count > 1)
  136.                         // You would want to leave 1 'duplicate' in
  137.     for (int i = 0; i < subList.Count - 1; i++)
  138.         // remove every instance from the array but one
  139.         copiedList.Remove(subList[i]);
  140. }
  141. int count = copiedList.Count; //this is your actual count