Sort Rows Randomly?

hellothere4

New Member
Joined
May 9, 2011
Messages
17
Hi, I wanted to sort rows randomly. My current thinking is to use rand() in column A after moving everything right a cell. However, I don't know how insert rand() only in rows which originally had a value. Can anyone help me with that? (in vba)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi,

If you're data is in consequetive rows with data in Column A to begin with, then the below should work for you:

Code:
Sub test()
Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Range("A1:A" & Lastrow).Formula = "=rand()"
End Sub

Cheers :)
 
Upvote 0
Code:
Sub Random_Sort()
    
    Application.ScreenUpdating = False
    Columns("A:A").Insert Shift:=xlToRight
    Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row).Formula = "=RAND()"
    Cells.Sort Key1:=Range("A1"), Order1:=xlDescending
    Columns("A:A").Delete Shift:=xlToLeft
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Code:
Sub Random_Sort_Rows()
    
    Application.ScreenUpdating = False
    Columns("A:A").Insert Shift:=xlToRight
    Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row).Formula = "=RAND()"
    Cells.Sort Key1:=Range("A1"), Order1:=xlDescending, Orientation:=xlTopToBottom
    Columns("A:A").Delete Shift:=xlToLeft
    Application.ScreenUpdating = True
        
End Sub

Sub Random_Sort_Columns()
    
    Application.ScreenUpdating = False
    Rows(1).Insert Shift:=xlShiftDown
    Range("A1", Cells(2, Columns.Count).End(xlToLeft).Offset(-1)).Formula = "=RAND()"
    Cells.Sort Key1:=Range("A1"), Order1:=xlDescending, Orientation:=xlLeftToRight
    Rows(1).Delete
    Application.ScreenUpdating = True
        
End Sub

Sub Random_Sort_Columns_and_Rows()

    Random_Sort_Columns
    Random_Sort_Rows
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,462
Members
452,915
Latest member
hannnahheileen

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