Multiplication Table VBA

Alcat03

New Member
Joined
Nov 7, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am doing a button where when you press it, it presents two numbers between 1 and 15 and wants the user to input the answer. If the user is wrong, it tells them incorrect try again if they get it right it tells them correct. I have this code written up but an error keeps popping up. I do not know where i went wrong.


Dim num1 As Integer
Dim num2 As Integer
Dim guess As Integer
Dim product As Integer


num1 = Application.RandBetween(1, 15)
num2 = Application.RandBetween(1, 15)

product = num1 * num2
guess = InputBox("num1 * num2 =")

Do While guess <> product
If guess > product Then
InputBox ("sorry, that is incorrect. try again: [num1] * [num2]")
ElseIf guess < product Then
InputBox ("sorry, that is incorrect. try again: [num1] * [num2]")
ElseIf guess = product Then
MsgBox "Correct. num1 * num2 = product"

VBA Code:
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You are missing an End If and a Loop statement.

You are prompting the user with "num1 * num2" instead of the actual numbers.

You don't need separate tests for > and < when the action is the same for both.

You give your user no way to quit.

You are not assigning the InputBox result to guess inside the loop.


Here is a rewrite.
VBA Code:
Sub mult()

   Dim num1 As Integer
   Dim num2 As Integer
   Dim guess As String
   Dim product As Integer
 
 
   num1 = Application.RandBetween(1, 15)
   num2 = Application.RandBetween(1, 15)
 
   product = num1 * num2
   guess = InputBox(num1 & " * " & num2 & " =")
   If guess = "" Then
      MsgBox "Time to quit"
      Exit Sub
   Else
 
      Do While guess <> product
            guess = InputBox("sorry, " & guess & " is incorrect. try again: " & num1 & " * " & num2 & " =")
            If guess = "" Then
               MsgBox "Time to quit"
               Exit Sub
            End If
      Loop
     
      MsgBox "Correct. " & num1 & " * " & num2 & " = " & product
 
   End If

End Sub
 
Upvote 0
Here is a cleaner version.
VBA Code:
Sub mult()

   Dim num1 As Integer
   Dim num2 As Integer
   Dim guess As String
   Dim product As Integer
   
   
   num1 = Application.RandBetween(1, 15)
   num2 = Application.RandBetween(1, 15)
   
   product = num1 * num2
  
   guess = InputBox(num1 & " * " & num2 & " =")
   Do Until guess = product
      guess = InputBox("sorry, " & guess & " is incorrect. try again: " & num1 & " * " & num2 & " =")
      If guess = "" Then
         MsgBox "Time to quit"
         Exit Sub
      End If
   Loop
      
   MsgBox "Correct. " & num1 & " * " & num2 & " = " & product

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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