Abstract

You like to generate a math test for your kid or for your class?

The input in sheet Main:

Generate_Math_Test1

The output in sheet Sample_Q_and_A:

Generate_Math_Test2

The output in sheet Sample_Q:

Generate_Math_Test3

Appendix – Generate_Math_Test.xlsm Code

Note: This program needs (uses) the Excel VBA class SystemState.

Please read my Disclaimer.

Option Explicit

Sub Random_Generation()
'Source (EN): http://www.sulprobil.com/generate_math_test_en/
'Source (DE): http://www.bplumhoff.de/generate_math_test_de/
'(C) (P) by Bernd Plumhoff  09-Jun-2022 PB V1.0
Dim vi, vTime
Dim lInputRow As Long
Dim lOutputRow As Long
Dim lNum As Long
Dim sExpr As String

Dim state As SystemState
Application.StatusBar = False
Set state = New SystemState

vTime = Now
Randomize
wsQA.Range("1:1048576").Delete
wsQ.Range("1:1048576").Delete
wsQA.[a1] = Range("Sample_Size") & _
            " Expressions and Results, generated " & _
            Format(vTime, "dddd dd-mmm-yyyy hh:mm")
wsQA.[a2] = "Expression"
wsQA.[b2] = "equals"
wsQA.[c2] = "Result"
wsQ.[a1] = Range("Sample_Size") & " Questions, generated " & _
           Format(vTime, "dddd dd-mmm-yyyy hh:mm")
wsQ.[a2] = "Expression"
wsQ.[b2] = "equals"
wsQ.[c2] = "?"

lInputRow = 2
sExpr = ""
Do While Not IsEmpty(wsMain.Cells(lInputRow, 1))
    If IsNumeric(wsMain.Cells(lInputRow, 1)) And _
        IsNumeric(wsMain.Cells(lInputRow, 2)) Then
        lNum = wsMain.Cells(lInputRow, 1) + Int(Rnd() * _
               (1 + wsMain.Cells(lInputRow, 2) - _
               wsMain.Cells(lInputRow, 1)))
        sExpr = sExpr & lNum
    Else
        sExpr = sExpr & wsMain.Cells(lInputRow, 1).Text
    End If
    lInputRow = lInputRow + 1
Loop
If IsError(Evaluate(sExpr)) Then
    wsMain.[d5] = "Expression """ & sExpr & _
                  """ evaluates to error!"
    Exit Sub
End If

For lOutputRow = 2 To 1 + Range("Sample_Size")
    lInputRow = 2
    sExpr = ""
    Do While Not IsEmpty(wsMain.Cells(lInputRow, 1))
        If IsNumeric(wsMain.Cells(lInputRow, 1)) And _
            IsNumeric(wsMain.Cells(lInputRow, 2)) Then
            lNum = wsMain.Cells(lInputRow, 1) + Int(Rnd() * _
                   (1 + wsMain.Cells(lInputRow, 2) - _
                   wsMain.Cells(lInputRow, 1)))
            sExpr = sExpr & lNum
        Else
            sExpr = sExpr & wsMain.Cells(lInputRow, 1).Text
        End If
        lInputRow = lInputRow + 1
    Loop
    wsQA.Cells(lOutputRow + 1, 1) = sExpr
    wsQA.Cells(lOutputRow + 1, 2) = "="
    wsQ.Cells(lOutputRow + 1, 1) = sExpr
    wsQ.Cells(lOutputRow + 1, 2) = "="
    If IsError(Evaluate(sExpr)) Then
        wsQA.Cells(lOutputRow + 1, 3) = "Expression """ & sExpr & _
                                        """ evaluates to error!"
        wsQ.Cells(lOutputRow + 1, 3) = "Expression """ & sExpr & _
                                       """ evaluates to error!"
    Else
        wsQA.Cells(lOutputRow + 1, 3) = Evaluate(sExpr)
        wsQ.Cells(lOutputRow + 1, 3) = "?"
    End If
Next lOutputRow

wsMain.[d5] = Range("Sample_Size") & _
              " Expressions and Results, generated " & _
              Format(vTime, "dddd dd-mmm-yyyy hh:mm:ss")

End Sub

Please read my Disclaimer.

Generate_Math_Test.xlsm [33 KB Excel file, open and use at your own risk]