Reputation: 23
I need to show on the same MsgBox
the multiplication table of a number chosen by a user.
Sub Multiplication1()
Dim number As Integer
Dim i As Integer
Dim res As Integer
number = InputBox(" Write number ")
For i = 1 To 10
res = number * i
MsgBox (number & " *" & i & " =" & res & vbCrLf)
Next
End Sub
In this code, for every loop iteration I have a now MsgBox
with the correct answer, I want all the answers on the same msgbox.
Upvotes: 0
Views: 851
Reputation: 37347
Few remarks: don't use Integer
, it's stored as Long
anyway in VBA and you don't need to declare all variables on separate lines :) see the code below. And to sdolve your problem, append every result to a string variable in a loop, after that display it in a message box.
Sub Multiplication1()
Dim number As Long, i As Long, res As Long, s As String
number = InputBox(" Write number ")
s = ""
For i = 1 To 10
res = number * i
s = s & (number & " *" & i & " =" & res & vbCrLf)
Next
MsgBox s
End Sub
Upvotes: 1
Reputation: 6368
You need to generate one string with the table, before display the message box:
Sub Multiplication1()
Dim number As Integer
Dim i As Integer
Dim res As Integer
Dim myTable as String
number = InputBox(" Write number ")
For i = 1 To 10
res = number * i
myTable = myTable & number & " *" & i & " =" & res & vbCrLf
Next i
MsgBox myTable
End Sub
Upvotes: 1