Debugging 为什么生成的随机数不存储在变量中
这是我的密码Debugging 为什么生成的随机数不存储在变量中,debugging,excel,vba,Debugging,Excel,Vba,这是我的密码 Sub lab1() Dim first As Integer Dim third As Integer Dim total As Integer total = 0 For i = 0 To i = 99 first = Application.WorksheetFunction.RandBetween(1, 100) total = total + first Next i MsgBox (total) End Sub 我试图生成100个介于1到10
Sub lab1()
Dim first As Integer
Dim third As Integer
Dim total As Integer
total = 0
For i = 0 To i = 99
first = Application.WorksheetFunction.RandBetween(1, 100)
total = total + first
Next i
MsgBox (total)
End Sub
我试图生成100个介于1到100之间的随机数,并将每个数存储到变量“total”中。我不明白为什么total的值总是低于100,似乎“total”只存储了1个随机数,而不是100个随机数,因为I=0到99VBA在
到之后需要一个数字,所以I=0到I=99的行被解释为对于I=0到(I=99)
,由于刚刚用值0
创建了i
,表达式i=99
等于False
,布尔值False
转换为0
因此,i=0到i=99的行被解释为i=0到0的,我不明白你的意思,我的代码和你的代码有什么区别??对于i=0到99,对于i=0到i=99,请检查你的代码。我注意到您的语法有一个错误。@joseph请告诉我哪一行,我对VBA编程一无所知
Dim first As Integer
Dim third As Integer
Dim total As Integer
total = 0
For i = 0 To 99
first = Application.WorksheetFunction.RandBetween(1, 100)
total = total + first
Next i
MsgBox (total)