VBA Array Sum Value Each Iteration

qeni

New Member
Joined
Jul 28, 2011
Messages
2
Hi guys,

I'm fairly new to VBA and I'm having a hard time with how Excel handles arrays. I'm generating a random number of integers from 1 to 10, iterating 10 times. I can't seem to figure out how to sum each iteration and place the value in a new array. Here's the code I have so far:

Code:
Sub getRandom()
    Dim i As Integer, r As Integer
    Dim minV As Integer, maxV As Integer
    Dim size As Integer, iterate As Integer
    Dim sumAll As Long
    Dim myArray() As Variant
    
    Cells.Clear
    
    minV = 1200
    maxV = 1800
    size = 10
    iterate = 10
    
    
    For r = 1 To iterate
    sumAll = 0
        For i = 1 To size
            ReDim Preserve myArray(1 To i)
            myArray(i) = Int(((maxV - minV) + 1) * Rnd + minV)
            Cells(i + 5, r) = myArray(i)
        Next i
    Next r
    
End Sub

Any help would be appreciated.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
After running your code, this is what I got:

Excel Workbook
ABCDEFGHIJ
61624122715151698126313771446174415371508
71520144816611695180014291448135716171478
81548171812321554160613801628167117491412
91374167515561792120917701396142717011443
101381142414811747154517881580137412131362
111665177813791336126014411324175215261233
121208172315741617126113671311157917501346
131657123315891788168012961550157714581788
141689177013581346137012971248145716071236
151626141813671520122715881475125815011434
Sheet1


Although I don't see why you couldn't jut put formulas in the cell and convert to values.

Did you want a sum of each row?
 
Upvote 0
This should do what your code does:

Code:
Sub getRandom()
Dim minV As Long, maxV As Long
Dim size As Long, iterate As Long
    
Cells.Clear
minV = 1200
maxV = 1800
size = 10
iterate = 10
With Range("A6").Resize(size, iterate)
    .Formula = "=Int(" & maxV - minV + 1 & " * Rand() + " & minV & ")"
    .Value = .Value
End With
End Sub
 
Upvote 0
@ HOTPEPPER : Thanks for your reply. To answer your question, I am trying to store the sums of each column into a one dimensional array. In other words as I iterate each i calculation r times, the sum of each iteration r is stored as a value in an array.

I really don't need to display any values other than the sums of each i. I Googled VBA arrays but I'm getting a little lost. I think I am making this more complicated than it needs to be.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top