+ Reply to Thread
Results 1 to 16 of 16

Macro to do randomization and sorting

  1. #1
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Macro to do randomization and sorting

    Good Evening,

    I am a student and working on a study, which requires me to generate a sample of randomize participants from an existing excel file, and then sort them in ascending order. I have to repeat this process for all twelve months (12 tabs in the attached file). Could someone please suggest, if there can be a macro that can assist creating a new variable that will allow participants to have unique ID (Random number) and then sort the new variable for all months (12 tabs) together?

    Thank you very much,
    Ajang
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Macro to do randomization and sorting

    Hi -

    You can do something like;
    Please Login or Register  to view this content.
    event

  3. #3
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    Hello event 21,

    I tried using the Macro but it did not work. Could you please see? Thank you

  4. #4
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Macro to do randomization and sorting

    Hi -

    Can you post your file with the codes?

    event

  5. #5
    Forum Contributor
    Join Date
    05-29-2013
    Location
    MD
    MS-Off Ver
    Excel 365
    Posts
    148

    Re: Macro to do randomization and sorting

    Quote Originally Posted by ajang View Post
    Hello event 21,

    I tried using the Macro but it did not work. Could you please see? Thank you
    Worked for me. What are the steps you are following?

  6. #6
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Macro to do randomization and sorting

    Hi -

    Please change this line
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    event

  7. #7
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    I tried again.

    The way I do it is. I will start working with January Tab. After last column with data, I create a new variable called Random using formula "=RAND()" or "=RANDBETWEEN($A$2,$A$26). Then I copy and paste them as special to create a new variable called Random2 because the otherwise you cannot sort them in any order (please see attached file). So, I am thinking that this macro will also create a new variable. May be I am thinking wrong. Please advise.
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Macro to do randomization and sorting

    Hi -

    You need to saveas the file as macro-enabled workbook attached.

    open the attached file, enable macros and click Ctrl + m

    event
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    Thank you. But I believe I did not explain clearly what I meant. One more time please

    What I want to do is create a new variable called Random1 (using Rand Formula) (as in the January tab) which, will give each client a new specific unique number in each tab below (Feb, March, April and so on...) and also create Random2 for each month. Random2 is nothing but Random1 pasted as special because Random2 allows to sort participants in ascending order which Random1 does not. I am wondering if there is a Macro that will do it for all 12 tabs together rather than me doing it manually in each tab.

    Thank you very much.

  10. #10
    Forum Contributor
    Join Date
    05-29-2013
    Location
    MD
    MS-Off Ver
    Excel 365
    Posts
    148

    Re: Macro to do randomization and sorting

    If you use RAND or RANDBETWEEN then the number will not be unique (possibly). The macro posted above generates a unique value, and you can use it to sort with. For each tab.

  11. #11
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    Thank you duncandhu. I was using RANDBETWEEN in the beginning but it was giving duplicate values. On the forum, I was suggested to use RAND instead because it will not give duplicate values and thus I started using RAND. I am not sure what is going wrong with my file, when I use the posted macro it shuffles the client ID but does not generate unique value for me. I have used Macro before many times therefore, I am not sure why is it not generating unique value for me.

  12. #12
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    Just to clarify, I mean the macro does not create any new variable for me which, will have unique values for each participant. I do not want to change anything in any of original variables. Thnxx

  13. #13
    Forum Contributor
    Join Date
    05-29-2013
    Location
    MD
    MS-Off Ver
    Excel 365
    Posts
    148

    Re: Macro to do randomization and sorting

    I think I understand now.

    The macro generates values in column A.

    You want values in column M?

    Try this (with credit to event21)

    Please Login or Register  to view this content.
    Last edited by duncandhu; 01-29-2014 at 12:06 AM.

  14. #14
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    I just posted the clarification right before you replied. " The macro is changing the values in the Client ID (the first variable) which, could be unique. But what I have been asking is for Macro to create a new variable which, will have unique values for each participant. I do not want to change anything in any of original variables." I believe that is the confusion. Thnxx

  15. #15
    Forum Contributor
    Join Date
    05-29-2013
    Location
    MD
    MS-Off Ver
    Excel 365
    Posts
    148

    Re: Macro to do randomization and sorting

    Sorry, I think I was editing the last post when you wrote this. Please see my last post!

  16. #16
    Registered User
    Join Date
    10-13-2010
    Location
    Miami, US
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Macro to do randomization and sorting

    It works perfectly . Thank you very much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Block randomization using formulas
    By abousetta in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-12-2012, 10:46 AM
  2. Need help creating randomization chart
    By amccoart in forum Excel General
    Replies: 2
    Last Post: 05-18-2012, 11:29 AM
  3. [SOLVED] Data Randomization
    By Hannie1004 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-16-2006, 02:55 PM
  4. strange randomization
    By William Benson in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-10-2005, 05:05 PM

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