我需要在同一MsgBox
上显示用户选择的数字的乘法表。
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
在这段代码中,对于每个循环迭代,我现在都有一个MsgBox
,并且答案正确,我希望所有答案都在同一个msgbox上。
答案 0 :(得分:1)
您需要使用表生成一个字符串,之前显示消息框:
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
答案 1 :(得分:1)
很少注意:不要使用Integer
,它还是以Long
的形式存储在VBA中,您不需要在单独的行上声明所有变量:)请参见下面的代码。为了解决您的问题,请将每个结果循环添加到字符串变量中,然后在消息框中显示该结果。
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