Randomly mixing datas base on same columns..

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,280
Office Version
  1. 2013
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...
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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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]
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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
Back
Top