我需要能够计算数组中唯一字母的数量,例如:
[A01, B01, C01, A02, C02]
对于此示例,结果应为3.因此,我只需要在找到新字母的情况下包含该值。
这是在Word 2007用户表单中完成的。 关于完成这项任务的任何建议或例子?
答案 0 :(得分: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