Macro Based Test Randomizer for Beginners

eljewelz

New Member
Joined
Mar 14, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Good morning all. I am trying to create a Test Randomizer for our QA section. We have 100 Questions on our Questions Sheet (A1 - A100) and I would like to click a button that will populate 25 random questions from the Questions Sheet to our Test Sheet (A1 - A25). I have found other Macros on other forums but I don't know how to adjust it to my specific Excel. Any help will be much appreciated. Thank you.
 

Attachments

  • Test Generator.png
    Test Generator.png
    54.9 KB · Views: 10

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try on a copy.
VBA Code:
Sub RandomizeTest()
    Dim questionsSheet As Worksheet
    Dim testSheet As Worksheet
    Dim rngQuestions As Range
    Dim randomIndex As Integer
    Dim i As Integer
    Dim selectedQuestions(1 To 25) As String
    Dim selectedCount As Integer
    Dim question As String
    Dim isDuplicate As Boolean
    
    Set questionsSheet = ThisWorkbook.Sheets("Questions Sheet")
    Set testSheet = ThisWorkbook.Sheets("Test Sheet")
    
    testSheet.Range("A1:A25").ClearContents
    
    Set rngQuestions = questionsSheet.Range("A1:A100")
    
    Randomize
    selectedCount = 0
    
    Do While selectedCount < 25
        randomIndex = Int((rngQuestions.Rows.Count * Rnd) + 1)
        question = rngQuestions.Cells(randomIndex, 1).Value
        
        isDuplicate = False
        For i = 1 To selectedCount
            If selectedQuestions(i) = question Then
                isDuplicate = True
                Exit For
            End If
        Next i
        
        If Not isDuplicate Then
            selectedCount = selectedCount + 1
            selectedQuestions(selectedCount) = question
        End If
    Loop
    
    testSheet.Range("A1:A25").Value = Application.Transpose(selectedQuestions)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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