Skip to main content

Count the number of time a certain item appears in an array

This snippet will take an array and a string value (you can easily change it to other formats) and count how many times an item appears in the array.

Snippet
Sub Main
	Dim thisArray(5) As String
	
	thisArray(0) = "Item1"
	thisArray(1) = "Item2"
	thisArray(2) = "Item1"
	thisArray(3) = "Item1"
	thisArray(4) = "Item1"
	thisArray(5) = "Item2"
	
	MsgBox ArrayItemCount(thisArray(), "Item1")
	
End Sub

Function ArrayItemCount(ByRef myArray() As String, sCheckValue As String) As Integer
	Dim i As Integer
	Dim iCount As Integer
	
	iCount = 0
	
	For i = LBound(myArray) To UBound(myArray)
		If myArray(i) = sCheckValue Then iCount = iCount + 1
	Next i
	
	ArrayItemCount = iCount
End Function