+ Reply to Thread
Results 1 to 7 of 7

Generating a Random List of Questions & Answers - VBA?

Hybrid View

  1. #1
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,825

    Generating a Random List of Questions & Answers - VBA?

    I suspect that this might be quite easy for you VBA pros, but here goes!

    I have a file with two columns laid out thus:

    Excel 2016 (Windows) 32 bit
    A
    B
    1
    Q1 A1
    2
    Q2 A2
    3
    Q3 A3
    Sheet: Sheet1

    Column A contains questions and column B contains answers. I want to be able to enter my pairs of Qs and As and then, at the push of a button, generate random pairs of answers and questions thus:

    Excel 2016 (Windows) 32 bit
    C
    1
    A3
    2
    Q1
    3
    4
    A1
    5
    Q2
    6
    7
    A2
    8
    Q3
    Sheet: Sheet1

    The list of Qs and As needs to be any length - usually between 16 and 24 rows of data. The random output needs to ensure that no matching pair ends up together.

    Any ideas?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Generating a Random List of Questions & Answers - VBA?

    Duplicate posing
    Last edited by mikerickson; 02-01-2017 at 10:50 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Generating a Random List of Questions & Answers - VBA?

    I think this will do what you want.
    You should adjust the ranges to suit.
    If you want less than all the questions in the result, adjust the Size variable.

    Sub test()
        Dim baseData As Variant
        Dim randomizedData() As String
        Dim Size As Long
        Dim i As Long, temp As Variant, randIndex As Long
        
        baseData = Range("A1:b5").Value
        Size = UBound(baseData, 1)
        
        Rem randomly re-order the question/answers
        For i = 1 To Size
            randIndex = WorksheetFunction.RandBetween(1, Size)
            temp = baseData(i, 1)
            baseData(i, 1) = baseData(randIndex, 1)
            baseData(randIndex, 1) = temp
            temp = baseData(i, 2)
            baseData(i, 2) = baseData(randIndex, 2)
            baseData(randIndex, 2) = temp
        Next i
        
        Rem put into column array
        ReDim randomizedData(1 To 3 * Size, 1 To 1)
        For i = 0 To Size - 1
            randomizedData(3 * i + 1, 1) = baseData(i + 1, 1)
            randomizedData(3 * i + 2, 1) = baseData(i + 1, 2)
        Next i
        
        Rem put on sheet
        Range("C1").Resize(3 * Size, 1) = randomizedData
    End Sub

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,016

    Re: Generating a Random List of Questions & Answers - VBA?

    .
    Ali

    You've peaked my curiosity. Please explain, if you don't mind, the purpose of aligning a random answer to a random question ? Interesting.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Generating a Random List of Questions & Answers - VBA?

    The OP asks to randomize the question/answer pairs. (And then re-arrange them into a column.) The OP request (as I read it), does not change the correspondence between a question and its answer.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,918

    Re: Generating a Random List of Questions & Answers - VBA?

    But OP's image and line below indicate that Q & A must not be from same line.
    The random output needs to ensure that no matching pair ends up together.

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Generating a Random List of Questions & Answers - VBA?

    Maybe :
    Sub Test()
      Dim a(), b(), c As New Collection, i As Long, j As Long
      a = Range("A1").CurrentRegion.Value
      ReDim b(1 To UBound(a, 1))
    loop1:
      Set c = Nothing
      For i = 1 To UBound(b): c.Add i: Next i
      For i = 1 To UBound(b) - 1
    loop2:
          j = Int(Rnd * c.Count) + 1
          If c(j) <> i Then
             b(i) = c(j)
             c.Remove j
          Else
             GoTo loop2
          End If
      Next i
      If UBound(b) <> c(1) Then b(UBound(b)) = c(1) Else GoTo loop1
      Set c = Nothing
      For i = 1 To UBound(b): c.Add Array(i, b(i)): Next i
      ReDim b(1 To UBound(b) * 3, 1 To 1)
      For i = 1 To UBound(a, 1)
          j = Int(Rnd * c.Count) + 1
          b(3 * i - 2, 1) = a(c(j)(0), 2)
          b(3 * i - 1, 1) = a(c(j)(1), 1)
          c.Remove j
      Next i
      Columns("D").ClearContents
      Range("D1").Resize(UBound(b, 1)).Value = b
    End Sub
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

+ 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. [SOLVED] Random Generating list
    By jaycee66 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-02-2016, 06:55 PM
  2. Hide/Unhide rows based on Yes or No answers to questions by dropdown list.
    By Johny1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-12-2015, 06:57 PM
  3. Add difficulty level on a working random quiz questions/answers
    By freeofcost in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-14-2014, 09:47 AM
  4. Generating a list of Random numbers of random length...
    By Growl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 12:10 PM
  5. Refine a list of data based upon answers to dropdown questions in excel
    By sk8blitz23 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-17-2012, 12:51 PM
  6. random answers generated from a list
    By juicy fruit in forum Excel General
    Replies: 9
    Last Post: 04-11-2010, 07:22 AM
  7. generating a whole random # from a list
    By Arturo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2005, 07: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