Hi,
Please I need help in the following exercise. I have to poblate the Vector A(k, 1) with elements of the Array B(i, j) by transposing the elements without using the Transpose VBA function. That means that I have to create a formula that transposes B(i, j) for example for a 4x3 Array.
Code
- Option Explicit
- Option Base 1
- Sub JumbleArray()
- 'Place your code here
- Dim i As Integer, j As Integer, k As Integer, Temp As Integer
- Dim nr As Integer, nc As Integer, n As Integer, B() As Variant, A() As Variant, rn As Integer
- nr = Selection.Rows.Count
- nc = Selection.Columns.Count
- n = nr * nc
- ReDim B(nr, nc) As Variant
- ReDim A(n, 1) As Variant
- For i = 1 To nr
- For j = 1 To nc
- B(i, j) = Selection.Cells(i, j)
- Next j
- Next i
- k = 1
- For i = 1 To nr
- For j = 1 To nc
- A(k, 1) = B(j - i + nr * i - nr + 1, j)
- k = k + 1
- Next j
- Next i
- For j = 1 To n
- rn = WorksheetFunction.RandBetween(1, n - j + 1)
- Temp = A(n - j + 1, 1)
- A(n - j + 1, 1) = A(rn, 1)
- A(rn, 1) = Temp
- Next j
- For i = 1 To nr
- For j = 1 To nc
- B(i, j) = A(k, 1)
- k = k + 1
- Next j
- Next i
- End Sub