[b]
Function ArrayAdd(Array0, Array9, Optional Add As Boolean = True)[/b]
Dim ta1, ta2, array1Sub, array2Sub, taOut
Dim NumRows1 As Long, NumRows2 As Long, NumColumns1 As Long, NumColumns2 As Long
Dim Array1Base, iJ As Long
[color="blue"] 'For later assignment of appropriate base to output array[/color]
array1Sub = Array0: Array1Base = LBound(array1Sub)
'To convert an input range to a Variant() array
ta1 = Array0: ta2 = Array9
[color="blue"] 'Assign to variables for comparing size of input arrays[/color]
NumRows1 = ArrayCount(ColumnVector(ta1, 1)): NumRows2 = ArrayCount(ColumnVector(ta2, 1))
NumColumns1 = ArrayCount(RowVector(ta1, 1)): NumColumns2 = ArrayCount(RowVector(ta2, 1))
[color="blue"] 'If input arrays are the same size then exit function[/color]
If Not (NumColumns1 = NumColumns2 And NumRows1 = NumRows2) Then
MsgBox "This function accepts only equal sized arrays."
Exit Function
End If
[color="blue"] 'Convert to one-dimensional arrays for ease of manipulation[/color]
ta1 = MakeArray(ta1, 1): ta2 = MakeArray(ta2, 1)
'Establish appropriate bounds for the output array
ReDim taOut(1 To UBound(ta1))
[color="blue"] 'If addition is indicated by the optional argument then add the respective elements[/color]
If Add Then ' <==
For iJ = 1 To UBound(ta1)
taOut(iJ) = ta1(iJ) + ta2(iJ)
Next iJ
[color="blue"] 'Otherwise subtract them [/color]
Else
For iJ = 1 To UBound(ta1)
taOut(iJ) = ta1(iJ) - ta2(iJ)
Next iJ
End If
[color="blue"] 'Restore the output array to its original shape[/color]
taOut = ArrayReshape(taOut, NumRows1, NumColumns1)
[color="blue"] 'Assign to the output array the array base of the first input array[/color]
taOut = ConvertBase(taOut, LBound(array1Sub))
ArrayAdd = taOut [color="blue"] 'Return the array of added or subtracted elements[/color]
[b]End Function
[color="red"]Chú ý: Hai hàm ArrayReshape() & ConvertBase() là hai trong 47 hàm tại mục 2[/color][/b]