=IF(ROW()+B$1<13,(A2*(2/(B

paste this formula in B2 and copy it down and across

See attached file

Saqib

Copy-of-Xl0000018.xls

Solved

Posted on 2011-05-10

See the attachement

when I put the number 2 in my b1 cell,my B11 cell calculated the average of a11 and a12 then I used

the average to calculate the next cell. In my column 3, the c10 calculates the average of a12,a11,a10

Then I used the last periods of the cell c10 to calculate the other cells in column c.

My question is how could I do with VBA to put the number 4 in d1 then to calculate the average of a12,a11,a10,a9 in my d9 cell

expert-exchange.xlsx

when I put the number 2 in my b1 cell,my B11 cell calculated the average of a11 and a12 then I used

the average to calculate the next cell. In my column 3, the c10 calculates the average of a12,a11,a10

Then I used the last periods of the cell c10 to calculate the other cells in column c.

My question is how could I do with VBA to put the number 4 in d1 then to calculate the average of a12,a11,a10,a9 in my d9 cell

expert-exchange.xlsx

9 Comments

=IF(ROW()+B$1<13,(A2*(2/(B

paste this formula in B2 and copy it down and across

See attached file

Saqib

Copy-of-Xl0000018.xls

great but i forgot to told that the number of row can change but the last row will always be #div/0!

=IF(ROW()+B$1<13,(A2*(2/(B

Assumption: Your existing formula is correct..

```
Sub Fill_formula()
lastrow = ActiveSheet.Columns(1).Find(What:="#DIV/0!", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row - 1
For i = 2 To lastrow - 1
ActiveSheet.Columns(i).EntireColumn.Delete
ActiveSheet.Cells(1, i) = i
ActiveSheet.Cells(1, i).NumberFormat = "###0"
Next i
'Set Rng = ActiveSheet.Range("A1:A" & lastrow)
For r = 2 To lastrow
For i = 2 To lastrow - r + 1
If i = lastrow - r + 1 Then
ActiveSheet.Cells(r, i).Formula = "=SUM(A" & r & ":A" & lastrow & ")/" & i
ActiveSheet.Cells(r, i).NumberFormat = "#,##0.00"
Exit For
Else
ActiveSheet.Cells(r, i).Formula = "=(R[0]C[" & i * -1 + 1 & "]*(2/(" & i & "+1))+R[1]C[" & i - i & "]*(1-(2/(" & i & "+1))))"
ActiveSheet.Cells(r, i).NumberFormat = "#,##0.00"
End If
Next i
Next r
End Sub
```

```
Sub Fill_formula()
lastrow = ActiveSheet.Columns(1).Find(What:="#DIV/0!", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row - 1
max_column = 4 '<--- Change the column here
For i = 2 To max_column
ActiveSheet.Columns(i).EntireColumn.Delete
ActiveSheet.Cells(1, i) = i
ActiveSheet.Cells(1, i).NumberFormat = "###0"
Next i
For r = 2 To lastrow
For i = 2 To lastrow - r + 1
If i > max_column Then Exit For
If i = lastrow - r + 1 Then
ActiveSheet.Cells(r, i).Formula = "=SUM(A" & r & ":A" & lastrow & ")/" & i
ActiveSheet.Cells(r, i).NumberFormat = "#,##0.00"
Exit For
Else
ActiveSheet.Cells(r, i).Formula = "=(R[0]C[" & i * -1 + 1 & "]*(2/(" & i & "+1))+R[1]C[" & i - i & "]*(1-(2/(" & i & "+1))))"
ActiveSheet.Cells(r, i).NumberFormat = "#,##0.00"
End If
Next i
Next r
End Sub
```

```
Sub Fill_formula()
lastrow = ActiveSheet.Columns(1).Find(What:="#DIV/0!", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row - 1
For i = 2 To lastrow
If ActiveSheet.Cells(1, i).Value = "" Then Exit For
max_column = i
Next
For r = 2 To lastrow
For i = 2 To max_column
mcolumn = ActiveSheet.Cells(1, i).Value
If mcolumn = "" Or mcolumn > lastrow - r + 1 Then
ElseIf mcolumn = lastrow - r + 1 Then
ActiveSheet.Cells(r, i).Formula = "=SUM(A" & r & ":A" & lastrow & ")/" & mcolumn
ActiveSheet.Cells(r, i).NumberFormat = "#,##0.00"
'Exit For
Else
ActiveSheet.Cells(r, i).Formula = "=(R[0]C[" & i * -1 + 1 & "]*(2/(" & mcolumn & "+1))+R[1]C[" & i - i & "]*(1-(2/(" & mcolumn & "+1))))"
ActiveSheet.Cells(r, i).NumberFormat = "#,##0.00"
End If
Next i
Next r
End Sub
```

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Path of Workbook | 3 | 30 | |

Excel 2013 data visualization with a simple 2 column X and Y data points | 7 | 17 | |

excel totals from one column based on 'not null cell' in a different column | 7 | 13 | |

Dynamic Bar chart in Excel | 5 | 12 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**20** Experts available now in Live!