Randomly mixing datas base on same columns..

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,126
Good Day,
I have a data table like below starts from A2 to C19
Is it possible to ruffle those datas randomly by clicking a command button?
Every column will remain with same datas ony their rows will be changed...
Thank you...


NAME111
NAME212
NAME313
NAME414
NAME515
NAME616
NAME721
NAME822
NAME923
NAME1024
NAME1125
NAME1226
NAME1331
NAME1432
NAME1533
NAME1634
NAME1735
NAME1836

<colgroup><col span="3"></colgroup><tbody>
</tbody>
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

BobUmlas

Well-known Member
Joined
Mar 14, 2002
Messages
1,181
Have the macro put =RAND() in D1:D19, sort all by column D, then clear Column D.
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,963
Office Version
  1. 365
Platform
  1. Windows
Good Day,
I have a data table like below starts from A2 to C19
Is it possible to ruffle those datas randomly by clicking a command button?
Every column will remain with same datas ony their rows will be changed...

Try this:
I'm using column J as a temporary helper column, you may change that to suit


Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1083594a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1083594-randomly-mixing-datas-base-same-columns.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], va
Application.ScreenUpdating = [COLOR=Royalblue]False[/COLOR]
n = Range([COLOR=brown]"A"[/COLOR] & Rows.count).[COLOR=Royalblue]End[/COLOR](xlUp).Row - [COLOR=crimson]1[/COLOR]
[COLOR=Royalblue]ReDim[/COLOR] va([COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] n, [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]1[/COLOR])
    [COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] n
    Randomize
    va(i, [COLOR=crimson]1[/COLOR]) = WorksheetFunction.RandBetween([COLOR=crimson]0[/COLOR], n)
    [COLOR=Royalblue]Next[/COLOR]

Col = [COLOR=crimson]10[/COLOR] [I][COLOR=seagreen]'column J as helper column, change to suit[/COLOR][/I]

Cells([COLOR=crimson]2[/COLOR], Col).Resize(n, [COLOR=crimson]1[/COLOR]) = va
Range(Cells([COLOR=crimson]2[/COLOR], [COLOR=brown]"A"[/COLOR]), Cells(n + [COLOR=crimson]1[/COLOR], Col)).Sort key1:=Cells([COLOR=crimson]2[/COLOR], Col), order1:=xlAscending, Header:=xlNo
Cells([COLOR=crimson]2[/COLOR], Col).Resize(n, [COLOR=crimson]1[/COLOR]).ClearContents

Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
[/FONT]
 

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,126
Hello again,
I did some alteration to start from A1, and sort..

Code:
Private Sub CommandButton1_Click()

Dim n As Long, i As Long, va
Application.ScreenUpdating = False
n = Range("A" & Rows.Count).End(xlUp).Row - 1
ReDim va(1 To n, 1 To 1)
    For i = 1 To n
    Randomize
    va(i, 1) = WorksheetFunction.RandBetween(0, n)
    Next


Col = 10 'column J as helper column, change to suit


Cells(1, Col).Resize(n, 1) = va
Range(Cells(1, "A"), Cells(n + 1, Col)).Sort key1:=Cells(1, Col), order1:=xlAscending, Header:=xlNo
Cells(1, Col).Resize(n, 1).ClearContents




'sorting
    Columns("A:D").Select
    ActiveWorkbook.Worksheets("tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("tables").Sort.SortFields.Add Key:=Range("C1:C30") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("tables").Sort
        .SetRange Range("A1:D30")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
Application.ScreenUpdating = True
End Sub


Could you please let me know if the row amount increase like C50 where to I change from the code?
Thanks again.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,387
Messages
5,636,019
Members
416,892
Latest member
Bensch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top