【问题标题】:How do you sum incrementally down a column until a value is reached, then start the sum counter over from the last position using VBA如何逐步向下求和一列直到达到一个值,然后使用 VBA 从最后一个位置开始求和计数器
【发布时间】:2020-10-17 20:11:01
【问题描述】:

所以我试图对一列进行累积求和,直到达到一个值,然后在与该值列相邻的列中,添加某种文本,指示一个和的结束位置和一个新的开始位置。接下来,我想用 0 值从上次中断的地方重新开始求和,并继续这样做,直到没有更多的值可以求和。

这是我所拥有的,但它似乎无法正常工作(即使“~3815”的总和尚未达到,它也会过早地开始递增到下一步:

Private Sub NumberOfTrucks_Click()
  Dim j As Integer
  Dim i As Integer

  '~~> j stands for number of summed segments
    j = 1
    dbSumTotal = 0
    lastrow = Range("AU" & Rows.Count).End(xlUp).Row
    For i = 3 To lastrow Step 1
   '~~> in column AU are the numbers for the sum
        dbSumTotal = dbSumTotal + Cells(i, "AU").Value
   '~~> write value to cell and reset sum to 0
        If dbSumTotal >= 3815.9999999 Then
           dbSumTotal = 0
  '~~> for next sum raise the segment number
           j = j + 1
        End If
        Cells(i, "AX") = j
    Next i
    MsgBox ("Completed")
End Sub

【问题讨论】:

  • 您是否希望它在 AX 列中依次为 1、2、3、4、5 等,直到总和达到 3815,然后返回 1 并重新开始?如果是这样,设置j = 0 而不是1,在dbsumtotal = 0 行下方添加j = 0,然后在j = j + 1 行上方添加Else
  • 你似乎没有在任何地方写dbSumTotal。AX应该是总和还是行数?
  • 没关系,我知道问题出在哪里。给我一秒钟。

标签: excel vba loops sum


【解决方案1】:

增量总和

快速修复

Private Sub NumberOfTrucks_Click()
    
    Dim LastRow As Long
    Dim j As Long
    Dim i As Long
    Dim dbSumTotal As Double
    
    LastRow = Range("AU" & Rows.Count).End(xlUp).Row
    '~~> j stands for number of summed segments
    j = 1
    
    For i = 3 To LastRow
    '~~> in column AU are the numbers for the sum
        dbSumTotal = dbSumTotal + Cells(i, "AU").Value
        Cells(i, "AX") = j
        ' To make it more obvious, uncomment the following line.
        'Cells(i, "AY") = dbSumTotal
    '~~> write value to cell and reset sum to 0
        If dbSumTotal >= 3815.9999999 Then
        ' 3815.9999999 might be a left over when you were using '>',
        ' so I think the previous line should be:
        'If dbSumTotal >= 3816 Then
            dbSumTotal = 0
    '~~> for next sum raise the segment number
            j = j + 1
        End If
    Next i
    
    MsgBox ("Completed")

End Sub

【讨论】:

  • 嘿 VBasic2008,这对简单地告诉我 dbSumTotal 的计算没有按预期工作很有帮助;也就是说,它一直累加到数据的末尾,而不是在达到 3815 时重新开始。所以,我认为我的问题出在 dbSumTotal 中,我需要看看如何修复脚本的该语句。谢谢。
  • 你能解释一下这个解决方案有什么问题吗?
  • VBasic2008。我才意识到我的错误。它根本不在你的补充中。相反,它在我的应用程序中。我的数据有问题并解决了这个问题。它现在正在工作。你的补充帮助我到达那里。谢谢。
【解决方案2】:

我们到了。

你的问题是当你到达超过 3815 的行时,你添加到 j,然后写到列 AX。现在,我们写信给AX,然后添加到j

Private Sub NumberOfTrucks_Click()
  Dim j As Integer
  Dim i As Integer

  '~~> j stands for number of summed segments
    j = 1
    dbSumTotal = 0
    lastrow = Range("AU" & Rows.Count).End(xlUp).Row
    For i = 3 To lastrow Step 1
   '~~> in column AU are the numbers for the sum
        dbSumTotal = dbSumTotal + Cells(i, "AU").Value
   '~~> write value to cell and reset sum to 0
        If dbSumTotal >= 3815.9999999 Then
           dbSumTotal = 0
           Cells(i, "AX") = j
           j = j + 1
        Else
  '~~> for next sum raise the segment number
           Cells(i, "AX") = j
        End If
    Next i
    MsgBox ("Completed")
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多