简体   繁体   English

如何在每张纸上的特定行具有求和公式VBA?

[英]How to have sum formula VBA for a specific row on each sheet?

Hello so a part of my macro is going through each sheet and putting the sum of that column at the end of it on each of the sheets. 您好,因此宏的一部分正在遍历每张纸,并将该列的总和放在每张纸的末尾。 But currently it is printing the sum as is, I also want it to show the formula. 但是目前它按原样打印总和,我也希望它显示公式。

Sub GoNext()
    Dim i As Integer

    For Each ws In ThisWorkbook.Worksheets
    Columns("E:E").Select
    Selection.NumberFormat = "$#,##0.00"

    i = ActiveSheet.Index + 1
    If i > Sheets.Count Then i = 1
    Sheets(i).Activate

    If ActiveSheet.Name <> "CPOA Report Macro" Then
    If ActiveSheet.Name <> "Summary" Then
    LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row + 1
    Range("E" & LastRow).Font.Bold = True
    Range("E" & LastRow) = WorksheetFunction.Sum(Range("E2:E" & LastRow - 1))
    End If
    End If

    Next ws

End Sub

I suggest to avoid using .Select and .Activate : See How to avoid using Select in Excel VBA . 我建议避免使用.Select.Activate :请参阅如何避免在Excel VBA中使用Select You can access the sheets and ranges directly. 您可以直接访问图纸和范围。

Also your If statements can be combined by using And . 同样,您的If语句也可以使用And组合。

And you can use .Formula to set the formula for a cell. 您可以使用.Formula设置单元格的公式。

Option Explicit

Public Sub GoNext()
    Dim LastRow As Long

    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Columns("E").NumberFormat = "$#,##0.00"

        If ws.Name <> "CPOA Report Macro" And ws.Name <> "Summary" Then
            LastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row + 1
            ws.Range("E" & LastRow).Font.Bold = True
            ws.Range("E" & LastRow).Formula = "=SUM(E2:E" & LastRow - 1 & ")"
        End If
    Next ws
End Sub

I recommend to use meaningful procedure names like Sub SetFormatAndSumFormula or something like this. 我建议使用有意义的过程名称,例如Sub SetFormatAndSumFormula或类似的名称。 That makes your code much easier to read. 这使您的代码更易于阅读。

暂无
暂无

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

相关问题 为每个工作表VBA选择带有特定列的第一个空行 - Selecting first empty row with specific columns for each sheet VBA VBA使用相同行中单元格的和公式。 行号将随每个循环而变化 - VBA to use sum formula for cells in same row. Row number will change with each loop 在一张表中循环遍历范围,并为每个单元格设置自定义公式。 将数据放入新工作表 [excel][vba][bloomberg] - Loop through range in one sheet and have custom formula for each cell. Put data into a new sheet [excel][vba][bloomberg] 如何将行VBA Excel的特定部分复制到另一个工作表? - How to copy specific part of row VBA Excel to another sheet? Excel VBA-如何在工作表中的特定行上开始输出 - Excel VBA - How to start output on specific row in sheet VBA - 创建新工作表时更新总和公式 - VBA - update sum formula when creating a new sheet 写一个动态求和公式 vba 取另一张纸的范围 - Write a dynamic sum formula vba that takes range from another sheet Exce VBA如何生成从特定行开始并在最后一行停止的行数? 公式有问题 - Exce VBA how to generate a row count in that starts with specific row and stops at last row? Formula is flawed VBA - 将 CSV 中每一行的第一个值存储到变量中并移动到特定工作表 - VBA - Store first value of each row from CSV into a variable and move to specific sheet 当它引用的工作表更新时,如何替换公式以保留在 vba 中的特定单元格上 - How do I replace a formula to stay on a specific cell in vba when the sheet it references updates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM