【问题标题】:Autosum vba for each new sheet added to workbook添加到工作簿的每个新工作表的 Autosum vba
【发布时间】:2018-10-16 23:27:28
【问题描述】:

我当前的 VBA 为工作表中的预定义列提供求和函数,这些列在代码中指定和定义。这很好用,但是我每天都在这个工作簿中添加新的工作表,因此每天编辑代码以添加一个新的工作表和范围以求和是不可行的。

有没有一种方法可以编辑我当前的代码,以便它为工作簿中的每个工作表执行求和函数?我在下面附上了当前代码供参考。

    Sub AutoSum()
Sheets("MASTER ACCOUNT REVENUE").Select
Range("D4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(2, 0).Select
Dim cel1 As String, cel2 As String
cel1 = ActiveCell.Offset(-2, 0).End(xlUp).Address
cel2 = ActiveCell.Offset(-1).Address
ActiveCell.Value = "=sum(" & (cel1) & ":" & (cel2) & ")"
End Sub

【问题讨论】:

    标签: excel vba function sum


    【解决方案1】:

    是的,只需遍历床单。注意:这是best to avoid using .Select/.Activate

    Sub autoSum_AllSheets()
    Dim ws As Worksheet
    Dim cel1 As String, cel2 As String
    Dim firstCel As Range
    
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            Set firstCel = .Range("D4").End(xlDown).Offset(2, 0)
            cel1 = firstCel.Offset(-2, 0).End(xlUp).Address
            cel2 = firstCel.Offset(-1).Address
            firstCel.Value = "=SUM(" & cel1 & ":" & cel2 & ")"
        End With
    Next ws
    
    End Sub
    

    注意:我知道 Offset() 部分中的冗余,但只是将它们保留给 OP,以了解如何更轻松地避免 .Select/.Activate。

    编辑:要遍历一堆列,一种(尽管很笨拙)的方法是将列字母添加到数组中:

    Sub autoSum_AllSheets()
    Dim ws As Worksheet
    Dim cel1 As String, cel2 As String
    Dim firstCel As Range
    
    Dim cols() As Variant
    cols = Array("D", "E", "F")
    
    Dim i As Long
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            For i = LBound(cols) To UBound(cols)
                Set firstCel = .Range(cols(i) & "4").End(xlDown).Offset(2, 0)
                firstCel.Select
                cel1 = firstCel.Offset(-2, 0).End(xlUp).Address
                cel2 = firstCel.Offset(-1).Address
                firstCel.Value = "=SUM(" & cel1 & ":" & cel2 & ")"
            Next i
        End With
    Next ws
    
    End Sub
    

    但请注意,如果 Column 在第 5 行之后的单元格中没有有任何信息,您将收到错误消息(因为 .XlDown 转到最后一行,您可以'然后从那里Offset(2,0)。)

    【讨论】:

    • 谢谢! @BruceWayne 你的建议奏效了。如果我想添加我想总结的第二个列范围,我将如何将它添加到 vba?
    • 感谢您的建议,但是我收到运行时错误 1004:应用程序定义或对象定义错误。我对您建议的已编辑代码所做的是在 cols = Array("J3", "C3") 部分中添加列范围起点。为了使代码正常运行,我还需要更改什么吗?
    • @NHure92 - 您是否将其更改为字面意义上的cols = Array("J3", "C3") 并留下其余部分?如果是这样,那么您应该将Set firstCel = ... 更改为... = .Range(cols(i)).End(xlDown).Offset(2,0)
    【解决方案2】:

    是的,添加:

    Dim wscount as long
    dim i as long
         wscount = Activeworkbook.Worksheets.Count
     for i = 1 to wscount
    Sheets(i).Select
    Range("D4").Select
     Selection.End(xlDown).Select
    ActiveCell.Offset(2, 0).Select
    Dim cel1 As String, cel2 As String
    cel1 = ActiveCell.Offset(-2, 0).End(xlUp).Address
    cel2 = ActiveCell.Offset(-1).Address
    ActiveCell.Value = "=sum(" & (cel1) & ":" & (cel2) & ")"
    next i 
    

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多