dimanche 29 septembre 2019

Trouble in creating a sub routine that completely randomizes a pre existing array sub routine in visual basics - excel

I need to use a VBA sub that randomly sorts a pre-existing selected array by leveraging the randbetween function in the sub.

If anyone has any suggestion it would be great, as I am a bit stuck here. Please see below what I have so far.

Option Explicit
Option Base 1

Sub shuffleArray()
Dim N As Integer, rn As Long
Dim j As Integer, i As Integer
Dim nr As Long, nc As Integer
Dim temp As Integer
Dim A()
nr = Selection.Rows.Count
nc = Selection.Rows.Count
N = Selection.Cells.Count
ReDim A(nr, nc)

rn = Application.WorksheetFunction.RandBetween(1, N - j + 1)

 For i = 1 To nr  

   For j = 1 To nc

       A(i, j) = Selection.Cells(i, j)

        temp = A(N - j + 1, i)
        A(N - j + 1, i) = A(rn)

        A(rn) = temp

   Next j
  Next I
End Sub



Aucun commentaire:

Enregistrer un commentaire