I want to automatically fill a 5 x 2
range with text and corresponding formulas. Consider we take columns D
and E
. The resulting range of cells should look as follows:
| D | E |
|---------|-------------------------------|
| Average | =ROUNDUP(AVERAGE($B2:$Bxx),0) |
| Maximum | =MAX($B2:$B41) |
| Median | =ROUND(MEDIAN($B2:$B41); 0) |
| Total | =COUNTA(Sheet!A$2:A$90000) |
| Total2 | =COUNTA('Sheet {2}'!A2:A8000) |
I thought that I could fill the whole range with pairs text, formula. I've created the following code:
Sub FillSummary()
Dim strFormulas(1 To 10) As Variant
Dim wsa As Worksheet: Set wsa = ActiveSheet
With wsa
strFormulas(1) = "Average"
strFormulas(2) = "=ROUNDUP(AVERAGE($B2:$Bxx),0)"
strFormulas(3) = "Maximum"
strFormulas(4) = "=MAX($B2:$Bxx)"
strFormulas(5) = "Median"
strFormulas(6) = "=ROUND(MEDIAN($B2:$Bxx); 0)"
strFormulas(7) = "Total"
strFormulas(6) = "=COUNTA(Sheet!A$2:A$90000)"
strFormulas(9) = "Total2"
strFormulas(10) = "=COUNTA('Sheet {2}'!A2:A8000)"
.Range("D2:E2").Formula = strFormulas
.Range("D2:E6").FillDown
End With
End Sub
Important moment here: I need to set different cell addresses every time the code runs. That is why I have specified Bxx instead of specific addresses.
I planned to use Replace() after the formulas are added like:
strFormulas(1) = Replace(xx, "41", sheetName)
However, the code fails when executing line .Range("D2:E2").Formula = strFormulas
. What am I doing wrong?
This code runs OK:
Sub FillDAUSummary()
Dim strFormulas(1 To 10) As Variant
Dim wsa As Worksheet: Set wsa = ActiveSheet
With wsa
.Range("D2").Formula = "Average"
.Range("E2").Formula = "=ROUNDUP(AVERAGE($B2:$B41),0)"
.Range("D3").Formula = "Maximum"
.Range("E3").Formula = "=MAX($B2:$B41)"
.Range("D4").Formula = "Median"
.Range("E4").Formula = "=ROUND(MEDIAN($B2:$B41), 0)"
.Range("D5").Formula = "Total"
.Range("E5").Formula = "=COUNTA(Sheet!A$2:A$90000)"
.Range("D6").Formula = "Total2"
.Range("E6").Formula = "=COUNTA('Sheet {2}'!A2:A8000)"
End With
End Sub
However, it has a shortcoming. I had to specify string range: B$2:$B41
. In fact, I would like to specify the end of the range on every run. Thank you.
Sub FillSummary()
Dim strFormulas(1 To 5, 1 To 2) As Variant
Dim wsa As Worksheet: Set wsa = ActiveSheet
Dim rw As Long
rw = 41
With wsa
strFormulas(1, 1) = "Average"
strFormulas(1, 2) = "=ROUNDUP(AVERAGE($B2:$B" & rw & "),0)"
strFormulas(2, 1) = "Maximum"
strFormulas(2, 2) = "=MAX($B2:$B41)"
strFormulas(3, 1) = "Median"
strFormulas(3, 2) = "=ROUND(MEDIAN($B2:$B41), 0)"
strFormulas(4, 1) = "Total"
strFormulas(4, 2) = "=COUNTA(Sheet!A$2:A$90000)"
strFormulas(5, 1) = "Total2"
strFormulas(5, 2) = "=COUNTA('Sheet {2}'!A2:A8000)"
.Range("D2:E6").Formula = strFormulas
End With
End Sub
OR
Sub FillSummary()
Dim strFormulas(1 To 5, 1 To 2) As Variant
Dim wsa As Worksheet: Set wsa = ActiveSheet
Dim rw As Long
rw = 41
With wsa
strFormulas(1, 1) = "Average"
strFormulas(1, 2) = "=ROUNDUP(AVERAGE($B2:$Bxx),0)"
strFormulas(2, 1) = "Maximum"
strFormulas(2, 2) = "=MAX($B2:$B41)"
strFormulas(3, 1) = "Median"
strFormulas(3, 2) = "=ROUND(MEDIAN($B2:$B41), 0)"
strFormulas(4, 1) = "Total"
strFormulas(4, 2) = "=COUNTA(Sheet!A$2:A$90000)"
strFormulas(5, 1) = "Total2"
strFormulas(5, 2) = "=COUNTA('Sheet {2}'!A2:A8000)"
strFormulas(1, 2) = Replace(strFormulas(1, 2), "xx", rw)
.Range("D2:E6").Formula = strFormulas
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.