+ Reply to Thread
Results 1 to 8 of 8

Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

  1. #1
    Registered User
    Join Date
    06-16-2020
    Location
    Colombia
    MS-Off Ver
    2016
    Posts
    11

    Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    Hi guys I need some help with this:


    Create a VBA subroutine named JumbleArray that will completely randomize/jumble a preexisting (m x n) array.

    This is an example of what you are trying to accomplish:

    1.PNG

    Durstenfeld’s Algorithm is an easy way to randomize an (ntot x 1) vector of ntot total elements (ntot = the product of m and n, or m x n). In this problem, you are asked to randomize an array – multiple rows and columns, so you'l have to first convert the array to a vector. In the Assignment 1 folder you will find a document called “Durstenfeld’s Algorithm.pdf”. Here is a sample flow diagram for the implementation of Durstenfeld’s Algorithm, although you will have to modify this a little bit to implement into your subroutine.

    HINT: The "rn = choose random number between 1 and (n-j+1)" line has confused many learners! This means that each time through, your algorithm needs to reassign a new random number between 1 and (n-j+1). You can do this using WorksheetFunction.RandBetween(1,n-j+1).

    2.PNG

    Your solution should utilize Durstenfeld’s Algorithm. However, you will first have to “decompose” your (m x n) array (I'll call this array D) into an (ntot x 1) vector (I'll call this vector A), as shown here:

    3.PNG

    Note that for this example, ntot would be (4 x 3) = 12. Each column is kept together, and columns are just “stacked” on top of each other. HINT: You should come up with a simple formula in which you can populate A as a function of i (row index), j (column index), and number of rows, and it will reference array D. Once A is populated, you can perform Durstenfeld’s Algorithm on A to completely randomize it.

    To do this "stacking", you'll want to come up with a simple single formula that relates i, j, and k, where k will go from 1 to ntot. Maybe something like this:

    k = 1

    For i = 1 To nr

    For j = 1 To nc

    A(k) = < some function of i, j, and nr, and you should reference array D >

    k = k + 1

    Next j

    Next i

    There are other ways to do this, but this might be the most intuitive. Next, you'll perform Durstenfeld's Algorithm on vector A, and the final step is to “recompose” the original (m x n) array (but now randomized) by reversing the decomposition process (by using a formula similar to the one you used for “stacking”, above, but for the reverse - I'll let you come up with that!).

    Your sub should work on selections that have words and not just numbers! Also, your subroutine should work on any size array anywhere on the worksheet.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    Is this a home work?

  3. #3
    Registered User
    Join Date
    06-16-2020
    Location
    Colombia
    MS-Off Ver
    2016
    Posts
    11

    Re: Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    Something like that but finally I could find a code with some of help:

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    Why so long?
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-16-2020
    Location
    Colombia
    MS-Off Ver
    2016
    Posts
    11

    Re: Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    Actually is so long because I´m taking VBA course, so... you know how teachers are... things must be done with the topics they explained.

    Thanks a lot!

  6. #6
    Registered User
    Join Date
    07-04-2020
    Location
    jharkhand
    MS-Off Ver
    10
    Posts
    2

    Re: Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    Hey buddy! can u please share the completion code of this assignment. I'm finding error in it. So please can you share the Completion code, if you dont mind.
    Thanks a lot

  7. #7
    Registered User
    Join Date
    06-16-2020
    Location
    Colombia
    MS-Off Ver
    2016
    Posts
    11
    Quote Originally Posted by nisarga View Post
    Hey buddy! can u please share the completion code of this assignment. I'm finding error in it. So please can you share the Completion code, if you dont mind.
    Thanks a lot
    I don't mind, but give some hours pls, I'm from Colombia and os too late here...so tomorrow morning I share you the code.

    Have a nice night or dar wherever you are!

  8. #8
    Registered User
    Join Date
    07-04-2020
    Location
    jharkhand
    MS-Off Ver
    10
    Posts
    2

    Re: Create a VBA subroutine named JumbleArray from a preexisting (m x n) array

    okay thanks a ton buddy
    Have a nice day

+ 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. Randomizing a vector from a preexisting array
    By analistobolsista in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-09-2020, 11:48 AM
  2. create array in named range to use in SUMIFS
    By esbencito in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-31-2018, 06:25 AM
  3. Create array in one cell as named range
    By jaryszek in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-21-2018, 12:39 AM
  4. [SOLVED] Why can't I assign an array variable to an array function in my subroutine?
    By redsab in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2017, 10:33 PM
  5. Create a named array
    By don.harvey in forum Excel General
    Replies: 2
    Last Post: 09-06-2009, 03:51 AM
  6. [SOLVED] Can you create dynamic named ranges containing array formulas?
    By Marie J-son in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2006, 09:10 AM
  7. subroutine to get all of the Named range properties
    By Arishy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2005, 11:05 AM

Tags for this Thread

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