+ Reply to Thread
Results 1 to 2 of 2

macro sort

Hybrid View

  1. #1
    Registered User
    Join Date
    06-14-2013
    Location
    atlants
    MS-Off Ver
    Excel 2007
    Posts
    18

    macro sort

    At the present I am using rand () sort with a string of numbers to sort randomize a column of names.

    Is there a macro to use this sort feature to randomize thw names without needing to manually hit sort.

    I have multiple lists that need to be randomized and it becomes a chore to hit sort for each one.

    Anyway I can automate this so I can accomplish the sort with out doing that. Im running excel 2003.

    Example worksheet 1 column b sort data a2: a:60 then move on to worksheet 2 column c sort the data then move onto worksheet d etc.

    I dont want the data replaced only randomized around.

    Thanks for reading

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: macro sort

    Hi
    try this

    it sorts the selected cells randomly. I assume you want to overwrite the contents of existing cells. If not, modify the
    Offset(0,0) to e.g. offset(0,1) to sort into the column adjacent on the right etc


    Sub random_sort()
    Dim MyCol As New Collection, MyCol2 As New Collection, x As Long, MyRange As Range, z As Long
    Set MyRange = Selection
    
    For x = 1 To MyRange.Cells.Count
        MyCol.Add MyRange.Cells(x).Value
    Next x
    
    For x = MyRange.Cells.Count To 1 Step -1
        y = Int((x * Rnd) + 1)
        MyCol2.Add MyCol(y)
        MyCol.Remove y
    Next x
    
    For z = 1 To MyRange.Cells.Count
        MyRange.Cells(z).Offset(0, 0) = MyCol2(z)
    Next z
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1