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代码,使其更具动态性。我想让sum计算更具动态性。所以我尝试像Range("A" & LastRow3).End(xlDown).Offset(0, 11).Formula = "=Sum(("M" & LastRow3).End(xlDown).Offset(0, 11) & lastRow44 & ")"一样自动将起始单元格定义为M50。然而,它并不像我想要的那样工作。有没有办法修改代码,使起始单元格的求和计算动态化?
谢谢!
发布于 2017-02-10 07:58:56
试着改变
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 & ")"您的公式首先设置为您定义的最后一行,然后向下搜索(就像您按下CTRL +向下箭头一样)。如果这不是您想要的,请尝试删除这两者的“.END(XlDown)”部分。
最后,如果您知道您使用的是偏移量11,为什么不将其设置为使用"M“而不是A,而不是偏移量?
发布于 2017-02-10 07:54:19
这样怎么样:
lastRow44 = Cells(Rows.Count, "A").End(xlUp).Row
For x = 50 To LastRow3
Range("A" & x).Formula = "=Sum(""M""" & x & "": M "" & lastRow44 & ")"
Next xhttps://stackoverflow.com/questions/42149060
复制相似问题