+ Reply to Thread
Results 1 to 4 of 4

help with for loops; wait for user input

Hybrid View

  1. #1
    Registered User
    Join Date
    12-08-2010
    Location
    Philippines
    MS-Off Ver
    Office 365
    Posts
    76

    help with for loops; wait for user input

    I'm trying to create a test engine that will load questions randomly for the question bank.

    Say I have 3 sheets

    1. Form Sheet = Where I have all of my form
    2. Question Sheet = Where I have all of my questions
    3. Order Sheet = Where I want to rearrange the questions randomly.

    I use vlookup for this function

    Say A1 is where I need to key in the question number so b1 will look it up on the table based on the value on A1 (Say 1 to 15)

    Now, I have it arranged at random in "order sheet"

    I just want to load the question number from 1 t 15 from the "Order Sheet" The problem that I have is the user needs to enter his answer before proceeding to the next question.


    I don't have the file with me because I cannot transfer it from my work computer.

    Here is my analogy

    Step 1: Arrange number
    Step 2: Load the first question
    Step 3: By clicking next, check if the answer is correct add 1 to the score if correct.
    Step 4: Load the next question
    Step 5: Enter answer and hit next
    Step 6: Load the next question until the user answer all of the questions.


    Sub nxt()
    
    dim a as integer
    
    for a = 2 to 10
    
    worksheets("Form").Cells(1,1).Value = worksheets("Order").Cells(a,1).Value
    
    'The question will change and the user needs to input an answer
    'After clicking next, the loop will continue.
    
    next
    I hope someone could help me or if you have a better approach with this, please suggest.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: help with for loops; wait for user input

    Book5.xls

    I think this is something like what you are looking for.

    Sub Quiz()
    Dim Question As Integer, Answer As String, Num As Integer, LastRow As Long, Correct As Integer
        
        'copy questions from database to test area
        Sheet2.Range("H1:I20").Value = Sheet2.Range("Q1:R20").Value
        Sheet2.Range("M2:N20").ClearContents
        
        For Question = 1 To 10
            'Transfer one random question
            LastRow = Sheet1.Cells(Rows.Count, "H").End(xlUp).Row
            Num = WorksheetFunction.RandBetween(2, LastRow)
            'Take random question from the bank (prevents repeat questions)
            Sheet2.Range("H" & Num & ":I" & Num).Select
            Selection.Cut
            'Add it to the questions asked list
            LastRow = Cells(Rows.Count, "M").End(xlUp).Row
            Sheet2.Range("M" & LastRow + 1).Select
            ActiveSheet.Paste
            'delete the cells the question came from
            Sheet2.Range("H" & Num & ":I" & Num).Delete Shift:=xlUp
            'Now ask the question
    Retry:
            Answer = InputBox(Sheet2.Range("M" & LastRow + 1).Value, "Question " & Question, "")
            If UCase(Answer) = "" Then GoTo Retry 'No answer given
            If UCase(Answer) = UCase(Sheet2.Range("N" & LastRow + 1).Value) Then 'Correct answer
                Correct = Correct + 1
            Else
            End If
            MsgBox Correct & " out of " & Question
            Sheet2.Range("O" & LastRow + 1).Value = Answer 'Store the given answer
        Next Question
        
    End Sub
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    12-08-2010
    Location
    Philippines
    MS-Off Ver
    Office 365
    Posts
    76

    Re: help with for loops; wait for user input

    Sub Quiz()
    Dim Question As Integer, Answer As String, Num As Integer, LastRow As Long, Correct As Integer
        
        'copy questions from database to test area
        Sheet2.Range("H1:I20").Value = Sheet2.Range("Q1:R20").Value
        Sheet2.Range("M2:N20").ClearContents
        
        For Question = 1 To 10
            'Transfer one random question
            LastRow = Sheet2.Cells(Rows.Count, "H").End(xlUp).Row
            Num = WorksheetFunction.RandBetween(2, LastRow)
            'Take random question from the bank (prevents repeat questions)
            Sheet2.Range("H" & Num & ":I" & Num).Select
            Selection.Cut
            'Add it to the questions asked list
            LastRow = Cells(Rows.Count, "M").End(xlUp).Row
            Sheet2.Range("M" & LastRow + 1).Select
            ActiveSheet.Paste
            'delete the cells the question came from
            Sheet2.Range("H" & Num & ":I" & Num).Delete Shift:=xlUp
            'Now ask the question
    Retry:
            Answer = InputBox(Sheet2.Range("M" & LastRow + 1).Value, "Question " & Question, "")
            If UCase(Answer) = "" Then GoTo Retry 'No answer given
            If UCase(Answer) = UCase(Sheet2.Range("N" & LastRow + 1).Value) Then 'Correct answer
                Correct = Correct + 1
            Else
            End If
            MsgBox Correct & " out of " & Question
            Sheet2.Range("O" & LastRow + 1).Value = Answer 'Store the given answer
        Next Question
        
    End Sub
    everything is great. I had a problem generating question then I suddenly noticed that you've referenced a wrong sheet. Thank you so much again. I need to custom fit this to my project.

  4. #4
    Registered User
    Join Date
    12-08-2010
    Location
    Philippines
    MS-Off Ver
    Office 365
    Posts
    76

    Re: help with for loops; wait for user input

    Thank you so much Andy. I'll try this code. Looks good.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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