Friday, July 6, 2012

Fill Array One D to Two D



Below function used to fill one dimensional array elements to 2D Array based on Given parameters,

==================================================================================================================================
Enum Combination
    xlRow
    xlColumn
End Enum

Enum FillType
    FillRowWise
    FillColumnWise
End Enum

==================================================================================================================================


Function JustifyArray(varArrInput As Variant, VarArrResult As Variant, TransformIn As Combination, HowMany As Long, HowToFill As FillType) As Boolean

      
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngTemp As Long
    Dim VarTemp()
    Dim lngCount
    Dim lngRowCount As Long
    Dim lngColCount As Long
    Dim blnFlag As Boolean
   
    If IsArray(varArrInput) Then
       
        If TransformIn = xlColumn Then
            lngCol = HowMany
            lngRow = Int(UBound(varArrInput) / lngCol) + 1
        ElseIf TransformIn = xlRow Then
            lngRow = HowMany
            lngCol = Int(UBound(varArrInput) / lngRow) + 1
        End If
    Else
    blngFlag = True
    End If
   
    If HowToFill = FillColumnWise Then
        lngTemp = lngRow
        lngRow = lngCol
        lngCol = lngTemp
    End If
   
    ReDim VarArrResult(lngRow, lngCol)
   
    lngTemp = LBound(varArrInput)
    For lngRowCount = 0 To lngRow
        For lngColCount = 0 To lngCol
            If lngTemp >= UBound(varArrInput) Then GoTo ExitEarly:
            VarArrResult(lngRowCount, lngColCount) = varArrInput(lngTemp)
            lngTemp = lngTemp + 1
        Next lngColCount
    Next lngRowCount
       
           
ExitEarly:
    If blnFlag Then
       
        VarArrResult = Null
        JustifyArray = False
    End If
End Function
==================================================================================================================================

Enjoy Reading …. 

No comments: