2017-04-10 92 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))'不會被編譯。 –