【问题标题】:VBA Code Sum the price of each customer itemsVBA代码总和每个客户项目的价格
【发布时间】:2021-12-07 06:53:58
【问题描述】:

我试图使用 VBA 代码对商品价格求和以找到总价和最终总价之间的差异,差异应该需要粘贴到可调整列中,但这里的输出通过迭代给出了所有总和值

这是我的代码:

Const start_row = 2
Const tot_price = 3
Const cus_id_num = 1
Sub refresh()
    Dim cus_id      As String
    Dim fee_item    As String
    Dim temp        As Variant
    
    i = start_row
    j = 1
    cus_id = Me.Cells(i, 1).Value
    While Me.Cells(i, 2).Value <> ""
        If Me.Cells(i, 1).Value <> "0" Then
            If cus_id <> Me.Cells(i, 1).Value Then
                cus_id = Me.Cells(i, 1).Value
            End If
            Debug.Print cus_id
            ''Debug.Print cus_id
            If cus_id <> "" Then
                If Me.Cells(i, tot_price).Value <> "" Then
                    base_fee = Me.Cells(i, tot_price).Value
                End If
                Debug.Print base_fee
                temp = 0
            Else
                If Me.Cells(i, 1).Value = "" Then
                    fee_item = Round(Me.Cells(i, tot_price).Value, 0)
                    temp = fee_item + temp
                End If
                Debug.Print temp
            End If
        End If
        i = i + 1
    Wend
End Sub

问题的数据:

Customer_id_number||Items            ||      Total Price || Adjustable
70                ||  groceries      ||         1578.00  ||  
----------------------------------------------------------------------
                   Detergent Powder             719.00
----------------------------------------------------------------------
                   Detergent Soap               58.00
----------------------------------------------------------------------
                   Floor cleaner liquid         396.00
 ---------------------------------------------------------------------
                   Tooth Paste                  58.14
 ---------------------------------------------------------------------
                   Tooth Brush                  27.51
 --------------------------------------------------------------------
                   Shampoo                      219.77
 ---------------------------------------------------------------------
                   Perfume                      100.00
 ---------------------------------------------------------------------
40                 vegetable and fruits         1370.00
----------------------------------------------------------------------
                   Tomatoes                     88.00
----------------------------------------------------------------------
                   Onions                       38.00
----------------------------------------------------------------------
                   Sweet Potatoes               93.00
----------------------------------------------------------------------
                   Oranges                      809.00
----------------------------------------------------------------------
                   Mangoes                      282.03
----------------------------------------------------------------------
                   Spinach                      59.90

图片: data image

输出: 70 第1578章

719

777

1173

1231

1259

1479

1579

40 1370

88

126

219

1028

1310

1370

我需要每次迭代的最终总和输出,有人可以帮我吗

非常感谢您!

【问题讨论】:

  • 我还没有完全理解您的要求,也没有完全理解您的代码做了什么或缺少了什么。无论如何,您是否考虑过为此使用数据透视表?
  • 你的问题很不清楚。请阅读minimal reproducible example 并给出适当的输入数据样本和所需的输出。屏幕截图也可能有帮助。在该样本数据上解释您到底想做什么。您是否考虑过使用SUMIFSUMIFS 之类的公式? • 您可能会受益于阅读Why is “Can someone help me?” not an actual question?
  • 当您说“客户项目”时,您是指每个“客户 ID_编号\项目”吗?
  • 对不起,我已经编辑了我的问题,你能帮忙解决一下吗
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: excel vba while-loop sum iteration


【解决方案1】:

如果我正确理解您的问题,您希望将每个客户的总数相加,最后,您还希望得到一个总计。

您为什么要使用 VBA? Subtotals 功能是 Excel 中的一项基本功能,如this official documentation 中所述,我举个例子:

数据:

CustID    Price
    70     1578
    70     719
    70     777
    70     1173
    70     1231
    70     1259
    70     1479
    70     1579
    40     1370
    40     88
    40     126
    40     219
    40     1028
    40     1310
    40     1370

小计使用情况:

结果要么显示每个客户的小计和总计,要么只显示小计和总计,或者只显示总计(点击数字 123 在左边距)。

这个“小计”功能的开始可以记录在一个宏中,你会得到类似的东西:

Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _
    Replace:=True, PageBreaks:=False, SummaryBelowData:=True

【讨论】:

  • 感谢您的回复!此表格是工具生成的,当用户在启用内容差异量粘贴到可调整列后下载表格时,它需要 VBA 代码来执行这些计算
  • ...您可以在宏中记录Subtotals 功能,并在每次更新文件时重播。
  • @vinay:我相应地编辑了我的答案,但是我不理解 VBA 命令中的所有参数。
  • 感谢@Dominique 的快速回复,小计在这里很有用,但我正在寻找 VBA 代码,它应该在每个客户的项目总和之间迭代循环,并应该检查总价格值和之间的差额上面给出的数据中的项目总和和差异量应粘贴在可调
  • 没问题。让我向您展示 Excel 的另一个功能(我假设“Customer_ID_Number”是 A 列?):选择整个 A 列,按“Ctrl+G”并转到空白。然后,在公式栏中,输入=A2(我猜您从第 2 行开始?),然后按“Ctrl+ENTER”(不仅仅是“ENTER”),所有客户 ID 都将填入上面的值(这使得过滤等其他功能非常容易)。
【解决方案2】:

请尝试下一个代码。未经测试,但如果我正确理解了问题,它应该可以工作:

Sub testSummaryzePerCustID()
   Dim sh As Worksheet, lastR As Long, arr, arrFin, i As Long, k As Long
   
   Set sh = ActiveSheet
   lastR = sh.Range("B" & sh.rows.count).End(xlUp).row
   arr = sh.Range("A2:C" & lastR).Value    'Place the range in an array for faster iteration
   ReDim arrFin(1 To UBound(arr), 1 To 1) 'reDim the final array to have the same number of rows as arr
   For i = 1 To UBound(arr)       'iterate between the array elements
        If arr(i, 1) = "" Then    'for cases of nullString in first column:
            If k = 1 Then         'first time it uses the value in the third column
                arrFin(i, 1) = arr(i, 3): k = k + 1
            Else
               arrFin(i, 1) = arrFin(i - 1, 1) + arr(i, 3) 'then it adds the values in the third column to the previous arrFin value
            End If
        Else
            arrFin(i, 1) = arr(i, 1) & " " & arr(i, 3): k = 1 'concatenate ID with value of the third column
        End If
   Next i
   'drop the final array content at once:
   sh.Range("D2").Resize(UBound(arrFin), 1).Value = arrFin
End Sub

如果您需要将返回的总和四舍五入以不显示小数(正如您在问题中向我们展示的那样),我可以调整代码来执行此操作。现在,它只添加它们并显示所有结果小数。

请进行测试并发送一些反馈。

【讨论】:

  • 谢谢@FaneDuru,但它给出了一个错误-下标超出范围,我在上面添加了图片以清除表格数据
  • 我正在开车...
  • 我忘了将 i 增加 k...我会在我在家的时候适应它。
  • 测试成功后请告诉我。
  • @Virat OK。我现在在家里。我将在 5 到 6 分钟内修改代码。我需要建立一个测试表...
猜你喜欢
  • 2013-02-16
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多