2013-05-02 35 views
1

我想总结所有的随机整数值超过500,然后将它们呈现在文本框中,但它不工作,每当我运行代码时,它总和零。这是使用VBA的用户表单的内部。任何建议,将不胜感激。试图添加随机整数的总和,VBA

Private Sub CommandButton1_Click() 
    Dim r As Double, c As Double, rand As Double, y As Double, x As Double, i As Double 
    r = TextBox1.Value 
    c = TextBox2.Value 
    rand = TextBox3.Value 
    Rnd [5] 
    i = 0 
     For x = 1 To r 
      For y = 1 To c 
       Cells(x, y).Value = Int(Rnd * rand) 
       If (ActiveCell.Value >= 500) Then 
        i = i + ActiveCell.Value 
       Else ' do nothing 
       End If 
      Next y 
     Next x 
     Cells(r + 1, c).Value = "SUM" 
     Cells(r + 1, c + 1).Value = i 
     MsgBox (i) 
End Sub 

回答

4

我不知道很多关于VBA,但可以

Cells(x, y).Value = Int(Rnd * rand) 
If (ActiveCell.Value >= 500) Then 

..可以参照不同的细胞?

+0

你是绝对正确的。我用** Cells(x,y).Value **替换了所有的** ActiveCell.Values **,并且它工作正常!感谢您解决解决这个年龄的奥秘! – 2013-05-02 07:01:04

+0

甜!我很高兴帮助:) – 2013-05-02 07:04:18

+0

如果这是答案,请帮助其他用户并将其标记为已接受。 – glh 2013-05-02 14:17:10