2017-04-10 36 views
0

我想构建一个集合,并从该集合中取出唯一值的计数,但在构建集合本身时出现错误。任何人都可以向我建议我错在哪里。请分享您的想法。请让我知道如何找出唯一值的计数。Excel VBA - 收集错误

Sub trial() 

Dim sampleVisualBasicColl As Collection 

For i = 2 To 10 

    Rng = Range("M" & i).value 

    StartsWith = Left(Rng, 3) 

    If StartsWith = "Joh" Then 

      sampleVisualBasicColl.Add Rng 

    Else 

    End If 

Next 

Debug.Print (sampleVisualBasicCol1) 

End Sub 
+0

刚才注意到 - 这不是你昨天问的重复吗? http://stackoverflow.com/questions/43314311/excel-vba-formula-counting-unique-value-error –

回答

1

使用集合只需将它添加到荷兰Joh收集和再算上项目:如果你想每个项目(约奔....​​..任何你有)的计数

'Using a collection 
Sub Col_test() 

    Dim cCol As Collection 
    Dim i As Long 

    Set cCol = New Collection 

    On Error GoTo Err_Handler 

    With ThisWorkbook.Worksheets("Sheet1") 
     For i = 2 To 20 
      If Left(.Cells(i, 13), 3) = "Joh" Then 
       cCol.Add .Cells(i, 13).Value, .Cells(i, 13).Value 
      End If 
     Next i 
    End With 

    Debug.Print cCol.Count 

    On Error GoTo 0 

Exit Sub 
Err_Handler: 
    Select Case Err.Number 
     Case 457 'This key is already associated with an element of this collection 
      Err.Clear 
      Resume Next 
     Case Else 
      MsgBox "Error " & Err.Number & vbCr & _ 
       " (" & Err.Description & ") in procedure Col_test." 
      Err.Clear 
    End Select 

End Sub 

然后使用字典:

'Using a dictionary. 
Sub Dic_Test() 

    Dim dict As Object 
    Dim i As Long 
    Dim sValue As String 
    Dim key As Variant 

    Set dict = CreateObject("Scripting.Dictionary") 

    With ThisWorkbook.Worksheets("Sheet1") 
     For i = 2 To 20 
      If Len(.Cells(i, 13)) >= 3 Then 
       sValue = Left(.Cells(i, 13), 3) 
       If dict.exists(sValue) Then 
        dict(sValue) = dict(sValue) + 1 
       Else 
        dict(sValue) = 1 
       End If 
      End If 
     Next i 
    End With 

    For Each key In dict.keys 
     Debug.Print key & " = " & dict(key) 
    Next key 

End Sub 

注:我使用Cells的代码中,而不是RangeCells(2,13)是M2(第13列第2行)。

我觉得这个环节非常有帮助的使用词典:https://excelmacromastery.com/vba-dictionary/

作为进一步的更新(后回答接受),并使用你在你的问题在这里献出名单:Excel VBA - Formula Counting Unique Value error这个代码字典将返回Joh = 4, Ian = 3

'Using a dictionary. 
Sub Dic_Test() 

    Dim dict As Object 
    Dim dictFinal As Object 
    Dim i As Long 
    Dim sValue As String 
    Dim key As Variant 
    Dim keyFinal As String 

    Set dict = CreateObject("Scripting.Dictionary") 
    Set dictFinal = CreateObject("Scripting.Dictionary") 

    'Get the unique values from the worksheet. 
    With ThisWorkbook.Worksheets("Sheet1") 
     For i = 2 To 20 
      If Len(.Cells(i, 13)) >= 3 Then 
       sValue = .Cells(i, 13).Value 
       If dict.exists(sValue) Then 
        dict(sValue) = dict(sValue) + 1 
       Else 
        dict(sValue) = 1 
       End If 
      End If 
     Next i 
    End With 

    'Count the unique values in dict. 
    For Each key In dict.keys 
     keyFinal = Left(key, 3) 
     If dictFinal.exists(keyFinal) Then 
      dictFinal(keyFinal) = dictFinal(keyFinal) + 1 
     Else 
      dictFinal(keyFinal) = 1 
     End If 
    Next key 

    For Each key In dictFinal.keys 
     Debug.Print key & " = " & dictFinal(key) 
    Next key 

End Sub 
+0

@Dareen Bartup-Cook谢谢你的手。但我正在寻找Unique Counts。不是计数。独一无二的约翰。说JohnRed,JohnRed,JohnBlue,JohnGreen然后唯一的约翰将是3. – Sid29

+0

啊,好的....所以JohnRed计为1.计数“Joh”,但结合确切的重复。 –

+0

是的,先生。这是我感到震惊。如何结合确切的重复?或以编程方式删除重复项, – Sid29

1

您需要创建集合并声明它。

Sub trial() 

Dim myCol As Collection 

Set myCol= New Collection ' creates the collection 

For i = 2 To 10 

    Rng = Range("M" & i).value 

    StartsWith = Left(Rng, 3) 

    If StartsWith = "Joh" Then 

      myCol.Add Rng 

    Else 

    End If 

Next 

For each x in myCol 
    Debug.Print x 
Next x 

End Sub 
+0

不,这也不会让我的收藏。 Debug.Print给我空的结果 – Sid29

+0

一方面你不能打印这样的集合,你需要指定你想打印的索引。另一方面,由于某种原因,它不喜欢你的收藏名称。尝试上面修改的代码,进行测试和工作。 – Absinthe

-1

您还没有声明变量Rng &我这些是最重要的事情要做。同时,我想提出这个公式,如果(Len(B2:B20)> 0,Match(B2:B20,B2:B20,0),“”) (Len(B2:B20)> Match(B2:B20,B2:B20,0),“”,))> 0,1))

其数组公式如此用Ctrl + shift + enter结束。

可以使用这其中也,

子COUNTUNIQUE()暗淡I,计数,J为整数计数= 1对于i = 1到470标志= False如果计数

1然后如果Sheet1.Cells(i, 3).Value = Sheet1.Cells(j,11).Value然后标记 = True结束如果下一个j否则标志=假结束如果如果标志=假然后Sheet1。单元格(计数, 11).Value = Sheet1.Cells(i,3).Value count = count + 1 End IfNext i Sheet1.Cells(1, 15).Value = count End Sub

+0

不,我认为这不会起作用,因为您还没有添加条件 - 仅以“Joh”开始 – Sid29

+0

对不起,这是我的错,我没有注意到标准,同时您很快就会使用公式我将能够得到解决方案。 –

0

嘿,这代码将帮助ü因为它收集在列表框中的唯一值,,

PRIVA TE子UserForm_Initialize() 昏暗cUnique为集合 昏暗RNG作为范围 昏暗小区作为范围 昏暗SH作为工作表 昏暗vNum作为变

组SH = ThisWorkbook.Sheets( “工作表Sheet”) 设置RNG = SH .Range(“A2”,sh.Range(“A2”)。Value =“John”。端(xlDown))

集cUnique =新集合

在错误继续下一步

针对每个小区在Rng.Cells cUnique.Add Cell.Value,CStr的(Cell.Value) 接着细胞

对错误转到0

对于每个vNum在cUnique Me.ListBox1.AddItem vNum

Next vNum End Sub

+0

'Set Rng = sh.Range(“A2”,sh.Range(“A2”)。Value =“John”.End(xlDown))'不会被编译。 –