+ Reply to Thread
Results 1 to 19 of 19

Generate Random Questions using VBA

  1. #1
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Generate Random Questions using VBA

    Hi Members

    I trying to generate 6 questions randomly from a sheet of 30 questions using a vba code.

    I have attached the macro based excel file..

    I'm able to generate the questions but certain times one question is getting missed out.

    I really don't understand why.


    For example it generates 1 to 5 questions correctly and then the 6th question does not generate.

    Please go through the excel code and let me know what i'm doing wrong.

    Thanks in advance.
    Attached Files Attached Files
    Last edited by meetgilbert; 07-21-2019 at 03:42 AM.

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

    Re: Generate Random Questions using VBA

    Your are code depends on the duplicates appearance.
    One duplicate means CountIf >0, it does nothing but the counter increases anyway.

    One way
    Please Login or Register  to view this content.

  3. #3
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: Generate Random Questions using VBA

    Ignore my last post
    Last edited by torachan; 07-21-2019 at 04:12 AM.

  4. #4
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Re: Generate Random Questions using VBA

    Thank you so much Forum Guru jindon

    Supppose i have more sheets and i need to include certain number of questions from each sheet, will the same code be good enough by changing only the sheet name.

    Correct me if i'm wrong.

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

    Re: Generate Random Questions using VBA

    In that case
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Re: Generate Random Questions using VBA

    vow this is getting interesting.

    sheet1 random 6 questions
    sheet2 random 8 questions
    sheet3 random 4 questions

    then please advice Forum Guru jindon

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

    Re: Generate Random Questions using VBA

    What do you mean?

    Random 6, 8, 4 questions from Shet1 2 3?
    or
    Random 6, 8, 4 questions to Sheet1 2 3?

  8. #8
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Re: Generate Random Questions using VBA

    Random 6 questions from Sheet1

    Random 8 questions from Sheet2

    Random 4 questions from Sheet3

    All generated questions should come in one sheet.

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

    Re: Generate Random Questions using VBA

    Try
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Re: Generate Random Questions using VBA

    Excellent. One last question.

    6 random questions from sheet1 range (A2:A10)
    8 random questions from sheet2 range (A5:A20)
    4 random questions from sheet3 range (A21:A30)

    Please advice bro.

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

    Re: Generate Random Questions using VBA

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Re: Generate Random Questions using VBA

    100 % Successful. Thank you so much for sharing your knowledge.

  13. #13
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62

    Re: Generate Random Questions using VBA

    if i need to insert a question title before generating random questions from each sheet how do i do it..

    for example:
    I. Answer the following questions:
    1.
    2.
    3.
    4.
    5.
    6.
    II. Choose the best answer:
    7.
    8.
    9.
    III. Answer in detail:
    10.
    11.
    12.

  14. #14
    Registered User
    Join Date
    07-19-2019
    Location
    india
    MS-Off Ver
    2013
    Posts
    18

    Re: Generate Random Questions using VBA

    Hi jindon, is it possible to run this code in endless loop, until i stop it manually, if yes could you please share the code.
    I mean that upon running the macro it will continue showing a new set of questions every few seconds and the loop will never stop.

  15. #15
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,882

    Re: Generate Random Questions using VBA

    @oggyoggy
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

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

    Re: Generate Random Questions using VBA

    Is this a last question?
    If you have more, ask now.

  17. #17
    Registered User
    Join Date
    07-27-2010
    Location
    India
    MS-Off Ver
    Excel 2019
    Posts
    62
    Quote Originally Posted by jindon View Post
    Is this a last question?
    If you have more, ask now.
    yes last and final.

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

    Re: Generate Random Questions using VBA

    Please Login or Register  to view this content.

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Another way …

    Quote Originally Posted by meetgilbert View Post
    if i need to insert a question title before generating random questions from each sheet how do i do it..

    for example:
    I. Answer the following questions:
    1.
    2.
    3.
    4.
    5.
    6.
    II. Choose the best answer:
    7.
    8.
    9.
    III. Answer in detail:
    10.
    11.
    12.
    If this example is the fixed part in column A so the easy way is to just allocate random in column B
    and that's needs a tiny permutation code like next demonstration according to your attachment
    (where you just need to modify the array variable V in order to fit your real workbook) :

    PHP Code: 
    Private Sub CommandButton1_Click()
        
    Dim VP&, C&, WN&, R&
            
    = [{"questions!A1:A14","B2:B7";"questions!A15:A22","B9:B11";"questions!A23:A30","B13:B15"}]
        For 
    1 To UBound(V)
                
    Application.Range(V(P1)).Count
                W 
    Evaluate("ROW(1:" ")")
            For 
    0 To Range(V(P2)).Count 1
                R 
    Application.RandBetween(1N)
                
    Range(V(P2))(1).Value2 Application.Range(V(P1))(W(R1)).Value2
                W
    (R1) = W(N1)
            
    Next
        Next
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

+ 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] Dynamically Generate a List of headers and questions
    By drivingguy in forum Excel General
    Replies: 4
    Last Post: 11-02-2018, 03:07 PM
  2. Replies: 1
    Last Post: 02-16-2016, 07:51 PM
  3. Randam Placement into cells based in conditions
    By bnwash in forum Excel General
    Replies: 9
    Last Post: 08-06-2014, 01:21 AM
  4. How to start from a specified number in Randam formula
    By roofi in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-06-2014, 03:38 AM
  5. Generate Random Math Questions
    By danoost in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-24-2012, 04:03 PM
  6. questions that aren't really excel questions
    By martindwilson in forum The Water Cooler
    Replies: 1
    Last Post: 03-11-2012, 08:21 PM
  7. generate a random number and use if function to generate new data
    By Dogdoc1142 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-25-2006, 10:50 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