The function Count3DigitNumbers counts each three digit number in a string separated by spaces.
It returns a string with the occurrence of each number.
Thus: "045 385 195 045 375 193 193 586 395 183 659 395"
Becomes: "045:2;183:1;193:2;195:1"
045 occurred 2x; 183 once; 193 2x; 195 once.
Run: Run_Count3DigitNumbers for an example.
Function Count3DigitNumbers(str As String) As String
Dim nArray(0 To 300) As Long, i As Long, nItem As Long
Dim sArray() As String
sArray = Split(str, " ")
For i = LBound(sArray) To UBound(sArray)
If IsNumeric(sArray(i)) Then
nItem = CLng(Val(sArray(i)))
If nItem >= 0 And nItem < 300 Then
nArray(nItem) = nArray(nItem) + 1
End If
End If
Next i
str = ""
For i = LBound(nArray) To UBound(nArray)
If nArray(i) > 0 Then
str = str & Format(i, "000") & ":" & nArray(i) & ";"
End If
Next i
If Len(str) Then str = Left$(str, Len(str) - 1)
Count3DigitNumbers = str
End Function
Sub Run_Count3DigitNumbers()
Dim sList As String
sList = Count3DigitNumbers("045 385 195 045 375 193 193 586 395 183 659 395")
sList = Replace(sList, ";", vbCr)
MsgBox sList
End Sub
Bookmarks