'********************************************************************************************************** ' Name: countArrayItems ' Author: mielk | 2012-06-21 ' ' Comment: Function to count the number of items within the given array. ' ' Parameters: ' arr Array which items are to be counted. ' ' Returns: ' Long The number of items within the given VBA array. ' ' ' Exceptions: ' NotArrayException ' Thrown if the given parameter is not an array. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2014-11-17 Artur Krawczyński Function created. '********************************************************************************************************** Public Function countArrayItems(arr As Variant) As Long Const METHOD_NAME As String = "countArrayItems" '------------------------------------------------------------------------------------------------------ Dim bDimensions As Byte Dim iDimension As Byte '------------------------------------------------------------------------------------------------------ 'Check if the given parameter [arr] is an array. If not, the code will be moved to 'NotArrayException label. If Not VBA.IsArray(arr) Then GoTo NotArrayException 'Function checks if the given array is declared (so if its number of dimensions is greater than 0). bDimensions = countDimensions(arr) 'If the given array is not declared, [bDimensions] variable is assigned with 1 as a base for 'multiplying process. If bDimensions Then countArrayItems = 1 'Multiply sizes of all dimensions of the given array in order to get the total number of items. For iDimension = 1 To bDimensions countArrayItems = countArrayItems * (UBound(arr, iDimension) - LBound(arr, iDimension) + 1) Next iDimension '========================================================================================================== ExitPoint: Exit Function '---------------------------------------------------------------------------------------------------------- NotArrayException: '(...) 'Put your own error handling here for a case if the given parameter is not an array. GoTo ExitPoint End Function '********************************************************************************************************** ' Name: countDimensions ' Author: mielk | 2012-03-03 ' ' Comment: Returns the number of dimensions of the given VBA array. ' ' Parameters: ' arr Array for which number of dimensions is to be returned. ' ' Returns: ' Integer The number of dimensions of the given VBA array. ' If the given value is not an array function returns -1. ' If the given value is declared as a dynamic array but its dimensions have not been ' declared yet, 0 is returned. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2012-03-03 mielk Function created. ' 2014-06-15 mielk Returned type changed to Integer to allow -1 to be returned. ' For non-arrays values -1 is returned. '********************************************************************************************************** Public Function countDimensions(arr As Variant) As Integer Const METHOD_NAME As String = "countDimensions" '------------------------------------------------------------------------------------------------------ Dim bound As Long '------------------------------------------------------------------------------------------------------ If VBA.IsArray(arr) Then On Error GoTo NoMoreDimensions Do bound = UBound(arr, countDimensions + 1) countDimensions = countDimensions + 1 Loop Else countDimensions = -1 End If '---------------------------------------------------------------------------------------------------------- NoMoreDimensions: End Function