excel 动态范围求和

72qzrwbm  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(143)

我试图对T到X的单元格求和,并将结果插入到单元格Y中。我从第50行开始,希望在工作表中最后一行结束,以便动态地工作。不幸的是,指定表中的最后一行后,宏停止工作。我读了很多教程,甚至复制了人们创建的相同宏,但它仍然不工作。

Subb Zad()
    Dim x As Long
    For x = 50 To Cells(Rows.Count, "Y").End(xlUp).Row
    Cells(x, "Y").Formula = Replace("=sum(T@:X@)", "@", x)

    Next
End Sub
2wnc66cl

2wnc66cl1#

无需循环,只需将相关公式一次应用于所有行:

Sub Zad()
    With ActiveSheet 'good practice to denote the parent sheet even if it is the active sheet
        Dim x As Long
        x = .Cells(.Rows.Count, "T").End(xlUp).Row 'Use T as Y may be empty
        .Range(.Cells(50,"Y"),.Cells(x,"Y")).Formula = "=SUM(T50:X50)"
    End With
End Sub

相关问题