简体   繁体   English

公式中的动态范围 VBA

[英]Dynamic Range In A Formula VBA

I am trying to find the SUM of the last 5 rows.我试图找到最后 5 行的总和。

This is my code:这是我的代码:

Sub SumRange()子总和范围()

Dim upperrang As Long
Dim lowerring As Long
Dim testi As Long

lowerring = Range("A1").End(xlDown).Row
upperrang = Range("A1").End(xlDown).Row - 5

testi = "=SUM(C& upperrang:C& lowerring)"
Range("D20").Value = testi

End Sub结束子

The error i'm getting is "type mismatch"我得到的错误是“类型不匹配”

Thanks for your help!谢谢你的帮助!

To sum the last 5 entries:总结最后 5 个条目:


Sub Test()

  Dim RngCnt As Long
  Dim I      As Long
  Dim Total  As Long

  RngCnt = Range("List").Count  #List is your Dynamic Range Name
  Total = 0

  For I = RngCnt To RngCnt - 4 Step -1
     Total = Total + Range("List").Cells(I, 1).Value
  Next I

End Sub

HTH HTH

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM