2014-03-18 84 views
0

我需要能夠計數的獨特的字母陣列的數量,如這樣的:在數組列表VBA查找和計數不同的字母

[A01, B01, C01, A02, C02] 

對於這個例子的結果應該是3,因此我需要僅在發現新信件的情況下包括該值。

這是在Word 2007用戶窗體中完成的。 任何建議或例子完成這樣的任務?

+0

僅限字母或字符? –

+0

Ia「a」與「A」不同? –

+0

它不需要區分大小寫。 – ukie

回答

1

這適用於我。運行Test()宏以獲取演示數據的結果。

Function UniqueLetterCount(ByRef Values() As String) As Integer 

    UniqueLetterCount = 0 
    Dim LettersSeen As String 
    Dim i As Integer 
    Dim Letter As String 
    LettersSeen = "" 

    For i = LBound(Values) To UBound(Values) 
     Letter = Left(Values(i), 1) 
     If InStr(LettersSeen, Letter) = 0 Then 'if Letter was not found in LettersSeen 
      UniqueLetterCount = UniqueLetterCount + 1 
      LettersSeen = LettersSeen & Letter 
     End If 
    Next 

End Function 


Sub Test() 

    Dim DemoArray(9) As String 
    DemoArray(0) = "A01" 
    DemoArray(1) = "A02" 
    DemoArray(2) = "B01" 
    DemoArray(3) = "C01" 
    DemoArray(4) = "A03" 
    DemoArray(5) = "D01" 
    DemoArray(6) = "D02" 
    DemoArray(7) = "C02" 
    DemoArray(8) = "B02" 
    DemoArray(9) = "C03" 
    MsgBox "Unique letter count is " & UniqueLetterCount(DemoArray) 

End Sub