Drew Donlon
Drew Donlon

Reputation: 11

Trying to add sums of random integers, VBA

I am trying to sum up all of the random integer values over 500 and then present them in a text box, however it is not working and whenever I run the code, it sums to zero. This is inside of a user form using VBA. Any suggestions would be appreciated.

 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

Upvotes: 1

Views: 145

Answers (1)

Arxo Clay
Arxo Clay

Reputation: 876

I don't know much about VBA, but could

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

.. be referring to different cells?

Upvotes: 4

Related Questions