Friday, July 6, 2012

Combine Array Elements [Rows or Columns]

here is an updated version of my last  function to combine arrays elements. by using this function user can combine columns as well as rows also , 


Enum Combination
    xlRow
    xlColumn
End Enum

Private Function CombineArrayCol(ArrFirst As Variant, ArrAdd As Variant, ArrResult As Variant, CombinationOf As Combination) As Boolean
       
      'Created By Rajan 4-July-2012
            
       '---------How to Use-------------------
       'Syntex
       'CombineArrayCol FirstArray ,SecondArray ,ResultArray
           
        '   FirstArray = FirstArray is in which second array need to be combine,Must have atleast on column and Multidimensional
        '   SecondArray = SecondArray is which need to combined with FirstArray,Must have atleast on column and Multidimensional
        '   ResultArray = ResultArray is in which combined array will be stored
        '   CombinationOf = what you want to Combine either Rows or Columns of Two Arrays
        ' This Function Return True if Operation is succesfull and False with NULL Result array if Operation is Not succeed
       
      Dim blnFlag As Boolean
     
      'Validating Array , IsArray
      If CombinationOf = xlColumn Then
        If IsArray(ArrFirst) And IsArray(ArrAdd) Then    ' if Both Parameter is an Array
        'Both array must contain same Row Number
            If UBound(ArrFirst) = UBound(ArrAdd) Then
                ' Both Array must contain atleast One Column
                If UBound(ArrAdd, 2) < 0 Or UBound(ArrFirst, 2) < 0 Then
                    blnFlag = True
                    GoTo ExitEarly:
                    End If
            Else
                    blnFlag = True
                    GoTo ExitEarly:
                End If
        Else
            blnFlag = True
            GoTo ExitEarly:
        End If
    Else
    
        If IsArray(ArrFirst) And IsArray(ArrAdd) Then    ' if Both Parameter is an Array
        'Both array must contain same Columns Number
            If UBound(ArrFirst, 2) = UBound(ArrAdd, 2) Then
                ' Both Array must contain atleast One Row
                If UBound(ArrAdd) < 0 Or UBound(ArrFirst) < 0 Then
                    blnFlag = True
                    GoTo ExitEarly:
                    End If
            Else
                    blnFlag = True
                    GoTo ExitEarly:
                End If
        Else
            blnFlag = True
            GoTo ExitEarly:
        End If
    
    End If


    'Defining Row and Column in ResultArray
    If CombinationOf = xlColumn Then
        ReDim ArrResult(LBound(ArrFirst) To UBound(ArrFirst), LBound(ArrFirst, 2) To (UBound(ArrFirst, 2) + UBound(ArrAdd, 2)))
    ElseIf CombinationOf = xlRow Then
        ReDim ArrResult(LBound(ArrFirst) To (UBound(ArrFirst) + UBound(ArrAdd)), LBound(ArrFirst, 2) To UBound(ArrFirst, 2))
    End If
   
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngRow2 As Long
    Dim lngCol3 As Long
   
    'Filling First Array in ResultArray
    For lngCol = LBound(ArrFirst, 2) To UBound(ArrFirst, 2)
        For lngRow = LBound(ArrFirst) To UBound(ArrFirst)
            ArrResult(lngRow, lngCol) = ArrFirst(lngRow, lngCol)
        Next lngRow
    Next lngCol
   
    
    If CombinationOf = xlColumn Then                                                         
    lngCol = lngCol - 1
    lngRow = 0
    ElseIf CombinationOf = xlRow Then
    lngRow = lngRow - 1
    lngCol = 0
    End If
   
    'Filling Second Array in ResultArray
    For lngCol2 = LBound(ArrAdd) To UBound(ArrAdd, 2)
        For lngRow2 = LBound(ArrAdd) To UBound(ArrAdd)
            ArrResult(lngRow2 + lngRow, lngCol2 + lngCol) = ArrAdd(lngRow2, lngCol2)
        Next lngRow2
    Next lngCol2
ExitEarly:
      If blnFlag Then
        ArrResult = Null
        CombineArrayCol = False
      Else
        CombineArrayCol = True
      End If
End Function


Enjoy reading.. 

Rajan

No comments: