繁体   English   中英

如何使用 Excel VBA 在单次运行中用一列中的 5 个文字和另一列中的 5 个公式填充 5 x 2 范围

[英]How do I fill a 5 x 2 range with 5 literals in one column and 5 formulas in another in a single run with Excel VBA

我想用文本和相应的公式自动填充5 x 2范围。 考虑我们采用DE列。 生成的单元格范围应如下所示:

| 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) |

我认为我可以用成对的文本、公式填充整个范围。 我创建了以下代码:

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

这里的重要时刻:每次代码运行时,我都需要设置不同的单元格地址。 这就是我指定 Bxx 而不是特定地址的原因。

我计划在添加公式后使用 Replace(),如下所示:

strFormulas(1) = Replace(xx, "41", sheetName)

但是,执行 line .Range("D2:E2").Formula = strFormulas时代码会失败。 我究竟做错了什么?

此代码运行正常:


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

但是,它有一个缺点。 我必须指定字符串范围: B$2:$B41 事实上,我想在每次运行时指定范围的结束。 谢谢你。

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

或者

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM