2013-05-02 41 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