【问题标题】:Sum totals based on specific value in another column excel VBA基于另一列excel VBA中特定值的总计
【发布时间】:2017-04-20 20:43:09
【问题描述】:

在 excel 中使用 VBA,我试图在 a 列中的特定文本之后添加一个新行,然后将 b 列中的成本相加。

这个新行应该采用指定的文本“apple”并在 a 列中添加单词 total。在 b 列中,它应该简单地将所有“苹果”相加。

A 列随机包含 3 种水果(苹果、橙子和香蕉)。

B 列包含每个苹果、橙子和香蕉的成本。

它应该看起来像这样

Apple $12    
Apple $12    
Apple $12    
orange $13    
orange $13    
Banana $7    
Banana $7

到

Apple $12    
Apple $12    
Apple $12    
Apple Total $36    
orange $13    
orange $13    
orange Total $26    
Banana $7    
Banana $7    
Banana Total $14

【问题讨论】:

  • Stack Overflow 不是我网站的代码,您尝试过什么?请在原始帖子中发布您的代码。

标签: excel vba


【解决方案1】:

您可能想要更改循环的边界(我只让它通过第 20 行)。当值也以货币表示时,这将起作用。

Dim i As Integer
Dim j As Integer
Dim frt As String

j = 1

For i = 2 To 20
    If Cells(i, 1).Value <> Cells(i - 1, 1).Value Then
        Range("A" & i).EntireRow.Insert
        frt = Cells(i - 1, 1).Value & " Total"
        Cells(i, 1).Value = frt
        Cells(i, 2).Value = Application.WorksheetFunction.Sum(Range("B" & j, "B" & i - 1))
        j = i + 1
        i = i + 1
    End If
Next i

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    相关资源
    最近更新 更多