I am having trouble with a VBA excel macro that I need to complete.
- I have n columns and rows (these amounts will always be different depending on the .csv file I want to process),
- I would like to be able to randomize the rows using a VBA macros
I know that =RAND()
would solve this, but this is what I would like to be able to automate and call when needed
Example table:
1 798 tea ...
2 889 coffee ...
3 990 mocca ...
4 282 latte ...
...
I would like the rows to be shuffled about like this: Example table:
1 282 latte ...
2 798 tea ...
3 889 coffee ...
4 990 mocca ...
...
This may be a trivial question to most, but I am new to VBA.
Here is what I have tried, but I am limited to explicitly typing the range each time, which I do not want to do.
Sub Shuffle()
Dim list As Variant
Dim rng As Range
Dim t As Long, j As Long, k As Long
t = 100
'The line below seems to be the problem, I don't know how to reference the range values?
Set rng = Range(Cells(1, 1), Cells(1, 1).End(xlDown))
list = rng.Value
t = UBound(list, 1)
j = t
Randomize
For i = 1 To t
k = Rnd() * j + 1
lngTemp = list(j, 1)
list(j, 1) = list(k, 1)
list(k, 1) = lngTemp
j = j - 1
Next
rng.Value = list
End Sub
Any help would be greatly appreciated.