lastRow44 = Cells(Rows.Count, "A").End(xlUp).Row
LastRow3 = Worksheets("Temp").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
Range("A" & LastRow3).End(xlDown).Offset(0, 11).Formula = "=Sum(M50:M" & lastRow44 & ")"
我正在尝试修改vba代码以使其更具动态性。我想将总和计算更具动态性。所以我正在尝试Range("A" & LastRow3).End(xlDown).Offset(0, 11).Formula = "=Sum(("M" & LastRow3).End(xlDown).Offset(0, 11) & lastRow44 & ")"
之类的东西
将起始单元格自动定义为M50。然而,这不符合我的意图。有没有办法修改代码来计算起始单元格的动态?
谢谢!
答案 0 :(得分:1)
尝试更改
lastRow44 = Cells(Rows.Count, "A").End(xlUp).Row
LastRow3 = Worksheets("Temp").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
到
lastRow44 = Sheets("Temp").Cells(Rows.Count, 1).End(xlUp).Row
LastRow3 = Worksheets("Temp").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
另外,我不确定你想要用
完成什么Range("A" & LastRow3).End(xlDown).Offset(0, 11).Formula = _
"=Sum(("M" & LastRow3).End(xlDown).Offset(0, 11) & lastRow44 & ")"
你的公式正在做的是首先设置你定义的lastrow,然后向下搜索(就像你按下CTRL +向下箭头一样)。如果这不是你想要的,请尝试删除" .END(xlDown"两者的一部分。
最后,如果你知道你使用的是11的偏移量,为什么不设置它使用" M"而不是A,而不是偏移?
答案 1 :(得分:0)
这样的事情怎么样:
lastRow44 = Cells(Rows.Count, "A").End(xlUp).Row
For x = 50 To LastRow3
Range("A" & x).Formula = "=Sum(""M""" & x & "": M "" & lastRow44 & ")"
Next x