2016-01-21 18 views
1

如何修改這個代碼也給空字符串範圍獲得獨特的價值觀以及在Excel VBA中的空字符串

Sub GetUniqueAndCount() 

    Dim d As Object, c As Range, k, tmp As String 

    Set d = CreateObject("scripting.dictionary") 
    For Each c In Selection 
     tmp = Trim(c.Value) 
     If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1 
    Next c 

    For Each k In d.keys 
     Debug.Print k, d(k) 
    Next k 

End Sub 

回答

1

只需刪除測試TMP的長度,它應該做的它:

Sub GetUniqueAndCount() 

    Dim d As Object, c As Range, k, tmp As String 

    Set d = CreateObject("scripting.dictionary") 
    For Each c In Selection 
     tmp = Trim(c.Value) 
     d(tmp) = d(tmp) + 1 
    Next c 

    For Each k In d.keys 
     Debug.Print k, d(k) 
    Next k 

End Sub 
+0

我不相信你可以設置一個字典鍵爲零長度的字符串或vbNullString。 – Jeeped

+0

我也不確定,但我試過d(「」)= 1,它工作。 –