vba - SumForumla : Two sumformula functions copied to two different cells in a "total" sheet

标签 vba excel

下面的宏创建 i新工作表的数量并将 E13 中的总计复制到单元格 F6 中的“总计”工作表中。我们还需要从“资源计算器”单元格 E14 中获取总计并将其复制到“总计”工作表单元格 F7。寻找一个好的方法来做到这一点的建议。提前致谢!

Sub Mac()
    Sheets("total").Visible = True

    Dim i As Integer, SumFormula As String
    For i = 1 To Sheet1.Range("A15").Value
        Sheets("Resource Estimator").Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Batch " & i

        If i = 1 Then
            SumFormula = "=SUM('" & ActiveSheet.Name & "'!E13" 'begin sum formula
        Else
            SumFormula = SumFormula & ",'" & ActiveSheet.Name & "'!E13" 'iterate sum formula
        End If
    Next i

    SumFormula = SumFormula & ")" 'end sum formula
    ThisWorkbook.Sheets("Total").Range("F6").Formula = SumFormula 'write sum formula to cell F6
End Sub

最佳答案

我将利用 3D 公式如下:

For i = 1 To Sheet1.Range("A15").Value
    Sheets("Resource Estimator").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Batch " & i
Next i

With Sheets("total")
    .Visible = True
    .Range("F6").Formula = "=sum('Batch 1:Batch " & i - 1 & "'!E13)"
    .Range("F7") = Sheets("Resource Estimator").Range("E14")
End With

顺便说一句,注意可能的工作表名称拼写错误:您的叙述讲述了“资源计算器”,但在您的代码中您编写了“资源估算器”。我假设它们是同一张纸,并且“资源估算器”作为它的名字

关于vba - SumForumla : Two sumformula functions copied to two different cells in a "total" sheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43207294/

相关文章:

excel - 如何将excel范围作为图片添加到outlook邮件正文

vba - Excel VBA - 第一行抛出 "Argument not optional"

java - 当我使用 poi 和 HSSFListener 处理记录时,如何获取 excel(xls) 单元格注释?

python - 识别缺失值并返回包含这些值的列表

python - Pandas /Excel : Any way to encode the ALT-ENTER/CHAR(10) line break into data when calling DataFrame. to_excel()?

excel - 使用 Excel VBA 在 OneDrive 上打开文件

excel - 如何从 VBA 函数触发错误

excel - "Case Else"在 VBA excel 中优先于其他 "Case"条件的问题

excel - 在excel中编辑长条件格式公式

arrays - 创建具有可变数量元素的数组变量