VBA Looping formula

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello, Fox, Hope you SAFE.

Formula.Average
The same formula must be entered in the
successive cells in a worksheet columns, but
One of the references, in the formula must be
incremente, by more than one row.

The spreadsheet contains 48 blocks of data,
in columns (“A:D”).
(“For illustration purpose I am showing 3 only”)

Each block is separated by four rows.

The average of each column
in the data block
Is to be calculated and entered
in the first 4 rows of columns (“F:I”).

Thus, the formula strings for rows 2 and 3 in column F must be
“=AVERAGE(A2:A6)” And
“AVERAGE(A11:A15
)”, respectively
the reference to the
row index in
these formulas
increases by 9, but the
Difference in the cells that hold the formulas
is only 1.

Therefore,
the correct formula for cell F3
cannot be obtained with a
copy and paste of cell F2 Because the
difference between the
row indices in
successive formulas is a constant.

But when I tried
a Variable -> J <- for the outer loop
do not work.
And the real data is for the whole year
48 weeks (or blocks).

VBA Code:
Sub Montecarlo_avr()

      Dim i As Integer
               For i = 0 To 2
                        Cells(i + 2, 6).Formula = "=Average(A" & (i + 2) + (i * 8) & ":A" & (i + 6) + (i * 8) & ")"
                        Cells(i + 2, 7).Formula = "=Average(B" & (i + 2) + (i * 8) & ":B" & (i + 6) + (i * 8) & ")"
                        Cells(i + 2, 8).Formula = "=Average(C" & (i + 2) + (i * 8) & ":C" & (i + 6) + (i * 8) & ")"
                        Cells(i + 2, 9).Formula = "=Average(D" & (i + 2) + (i * 8) & ":D" & (i + 6) + (i * 8) & ")"
               Next i

End Sub

Monte.PNG
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this:

VBA Code:
Sub Montecarlo_avr()
  Dim a As Range
  For Each a In Range("A1", Range("A" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants).Areas
    With Range("F" & Rows.Count).End(3)(2).Resize(, 4)
      .Formula = "=AVERAGE(" & a.Address(0, 0) & ")"
      .Value = .Value
    End With
  Next
End Sub

If you want only the formulas, then remove this line from the macro:
.value = .value

Dante Amor
ABCDEFGHI
1Week 1Average AAverage BAverage CAverage D
2595301350322357410.2340.2275.8
33294417063330.4289.8429.8385
421182497287389.2254.8134.4403.2
5147574427154
6693553357553
7
8
9
10Week 2
1138543456784
12399322637315
13427147203546
14126329203532
15315217539448
16
17
18
19Week 3
2036420398294
21385385350392
2221722442364
23609385175665
24371777301
Hoja2
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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