Random Questions from specified Category

Tunes81

New Member
Joined
Jul 28, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I am trying to create a quiz generator that will pull 10 random non duplicate questions from a question bank. I was able to get a random question from the bank by using this forumula: =INDEX(A2:A500,ROUND(MRAND()*COUNTA(A2:A500),1))
How can I get this formula to only give me a question that is in the same row as a specified category? Any help would be greatly appreciated!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Sorry this is the formula:
=INDEX(A2:A500,MROUND(RAND()*COUNTA(A2:A500),1))
 
Upvote 0
If you can use VBA a custom function can do that

VBA Code:
Function RandomQuestions(QuestionBank As Range, Optional NumberOfQuestions As Long = 10, Optional Category As String = "")
    Dim QB, QB1, i As Long, ub As Long, j As Long, idx As Long, k As Long
    QB = QuestionBank
    ub = UBound(QB)
    For i = 1 To ub
        If QB(i, 1) = Category Or Category = "" Then
            j = j + 1
            QB(j, 1) = QB(i, 2)
        End If
    Next
    If j = 0 Then
        RandomQuestions = "Category not found"
        Exit Function
    End If
    ReDim QB1(1 To j)
    ReDim Output(1 To NumberOfQuestions, 1 To 1)
    For i = 1 To j
        QB1(i) = QB(i, 1)
    Next
    For i = j To j - NumberOfQuestions + 1 Step -1
        k = k + 1
        If i > 0 Then
            idx = Int(Rnd() * i) + 1
            Output(k, 1) = QB1(idx)
            QB1(idx) = QB1(i)
        Else
            Output(k, 1) = ""
        End If
    Next
    RandomQuestions = Output
End Function

Cell Formulas
RangeFormula
H2:H6H2=RandomQuestions($A$2:$B$28,5,"D")
E2:E11E2=RandomQuestions($A$2:$B$28)
E13:E17E13=RandomQuestions($A$2:$B$28,5,D13)
B2:B28B2="Q"&ROW()-1 & A2
E19:E28E19=RandomQuestions($A$2:$B$28,11,"C")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top