I'm new on this forum and to VBA altogether, so forgive me if I mess something up.

I am working on creating a new matrix raised to a power function that gets called in the worksheet. I have looked around on different forums and found a good basis, but I'm running into something that seems to be unique to my circumstance.

What will happen, is the user will call =POWERMATRIX("Input Range", "Input Power"). The Input Range is given a name in the worksheet, called "Count". Count must always be a square matrix (otherwise you can't raise it to a power). When the entire Count range is continuous and all together, one solid square in the worksheet, everything works fine, I'm golden. However, when Count is discontinuous and has 2 or more areas, I run into issues.

What I'm trying to do is take each area of the Input Range and union them together into a single range, with one Area, so that I can get the function to work.

This latest iteration is what I've got. I've tried tons of things, but nothing seems to work.

Formula: copy to clipboard
Function PowerMatrix(inpRng As Range, pow As Integer)
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim rowSize As Integer
Dim colSize As Integer
Dim testArry() As Double
Dim testvalue As Integer
Dim rng As Range
Dim finalRng As Range
Dim counterMax As Integer

counterMax = inpRng.Areas.Count

For j = 1 To counterMax
finalRng = Union(finalRng, inpRng.Areas.Item(j))
Next j

' Some of the cells may not have numbers. This turns those cells to 0. Everything below here works perfect if it is a square matrix. I'm not concerned about anything below here.

rowSize = finalRng.Rows.Count
colSize = finalRng.Rows.Count

For i = 1 To rowSize
For j = 1 To colSize
If Application.WorksheetFunction.IsNumber(inpRng(i, j)) Then
testArry(i - 1, j - 1) = inpRng(i, j) 'inpRng will change to finalRng once I get a good union
Else
testArry(i - 1, j - 1) = 0
End If
Next j
Next i

PowerMatrix = testArry

For k = 1 To pow - 1
PowerMatrix = Application.WorksheetFunction.MMult(testArry, PowerMatrix)
Next k
End Function


I don't know if I need to call finalRng as a variant, or if I need to resize it, or what. I've tried each, but I must not be doing the syntax correctly on something.
In summary, I need to be able to join multiple areas of a range into a single range of 1 area. Help?