【问题标题】:Sum up column B based on colum C values根据 C 列的值对 B 列求和
【发布时间】:2016-04-17 20:49:55
【问题描述】:

我有一个简单的问题:如果第 1 列和第 3 列中的值匹配,我会尝试在包含 4 列第 2 列的表中求和。我在这里找到了关于堆栈溢出的示例代码,但它目前基于第 1 列计算。我是 VBA 新手,不知道要更改什么或如何调整代码以使我的计算基于第 1 列和第 3 列。这里是示例代码:

Option Explicit

Sub testFunction()
   Dim rng As Excel.Range
   Dim arrProducts() As String
   Dim i As Long

Set rng = Sheet1.Range("A2:A9")

arrProducts = getSumOfCountArray(rng)

Sheet2.Range("A1:B1").Value = Array("Product", "Sum of Count")

' go through array and output to Sheet2
For i = 0 To UBound(arrProducts, 2)
    Sheet2.Cells(i + 2, "A").Value = arrProducts(0, i)
    Sheet2.Cells(i + 2, "B").Value = arrProducts(1, i)
Next

End Sub

' Pass in the range of the products
Function getSumOfCountArray(ByRef rngProduct As Excel.Range) As String()
Dim arrProducts() As String
Dim i As Long, j As Long
Dim index As Long

ReDim arrProducts(1, 0)

For j = 1 To rngProduct.Rows.Count
    index = getProductIndex(arrProducts, rngProduct.Cells(j, 1).Value)
    If (index = -1) Then
        ' create value in array
        ReDim Preserve arrProducts(1, i)
        arrProducts(0, i) = rngProduct.Cells(j, 1).Value ' product name
        arrProducts(1, i) = rngProduct.Cells(j, 2).Value ' count value
        i = i + 1
    Else
        ' value found, add to id
        arrProducts(1, index) = arrProducts(1, index) + rngProduct.Cells(j, 2).Value
    End If
Next

getSumOfCountArray = arrProducts
End Function

Function getProductIndex(ByRef arrProducts() As String, ByRef strSearch As String) As Long
' returns the index of the array if found
Dim i As Long
For i = 0 To UBound(arrProducts, 2)
    If (arrProducts(0, i) = strSearch) Then
        getProductIndex = i
        Exit Function
    End If
Next

' not found
getProductIndex = -1
End Function

Sum Column B based on Column A using Excel VBA Macro

请您告诉我如何解决这个问题。您可以在下面找到我的小桌子的示例图片。比如黄色部分的数量要加总,第二行删掉。

Sample Table - Picture

【问题讨论】:

    标签: arrays vba excel macros sumifs


    【解决方案1】:

    您说“我尝试在一个包含 4 列第 2 列的表格中进行总结”,但从您的“示例表 - 图片”中,我知道您想要总结第 4 列

    在数据范围的 OP 变化后编辑

    假设以上你可以尝试以下

    Option Explicit
    
    Sub main()
    On Error GoTo 0
    
    With ActiveSheet '<== set here the actual sheet reference needed
    '    With .Range("A:D").Resize(.cells(.Rows.Count, 1).End(xlUp).row) '<== here adjust "A:D" to whatever colums range you need
        With .Range("A51:D" & .cells(.Rows.Count, "A").End(xlUp).row) '<== here adjust "A:D" to whatever colums range you need
            With .Offset(1).Resize(.Rows.Count - 1)
                .Offset(, .Columns.Count).Resize(, 1).FormulaR1C1 = "=SUMIFS(C2, C1,RC1,C3, RC3)" '1st "helper column is the 1st column at the right of data columns (since ".Offset(, .Columns.Count)")
                .Columns(2).Value = .Offset(, .Columns.Count).Resize(, 1).Value 'reference to 1st "helper" column (since ".Offset(, .Columns.Count)")
    
                .Offset(, .Columns.Count).Resize(, 1).FormulaR1C1 = "=concatenate(RC1,RC3)"
    
                With .Offset(, .Columns.Count + 1).Resize(, 1) '2nd "helper" column is the 2nd column at the right of data columns (since ".Offset(, .Columns.Count + 1)"
                    .FormulaR1C1 = "=IF(countIF(R1C[-1]:RC[-1],RC[-1])=countif(C[-1],RC[-1]),1,"""")" 'reference to 1st "helper" column (with all those "C[-1]")
                    .Value = .Value
                    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                    .Offset(, -1).Resize(, 2).ClearContents ' reference to both "helper" columns: ".Offset(, -1)" reference the 1st since it shifts one column to the left from the one referenced in the preceeding "With.." statement (which is the 2nd column at thre right of data columns) and ".Resize(, 2)" enlarges to encose the adjacent column to the right
                End With
            End With
        End With
    End With
    
    End Sub
    

    它使用了两个“帮助”列,我认为这可能是与最后一个数据列相邻的两个(即:如果数据列是“A:D”,那么帮助列是“E:F”)

    您是否需要使用不同的“帮助”列然后查看 cmets 了解它们的位置并相应地更改代码

    【讨论】:

    • 非常感谢@user3598756 ....是的,我的意思是根据交货编号和产品代码总结数量的列(如果这两个匹配,则总结).. ..我会试试这个谢谢...在E列中(作为下一步)将进行另一个计算..那么您认为辅助列会干扰吗?
    • 在 E 列中(作为下一步)将进行另一个计算....那么您认为辅助列会干扰吗? 不,它不会打扰,因为只有在我的“主”代码运行期间才需要“E”和“F”“助手”列。但我要再次指出,“main”将删除这两列中的所有内容,因此,如果在运行“sum-up”宏之前您要保留的“E”和“F”列中有数据,那么您必须选择两个不同的辅助列。顺便说一句,如果您发现我的答案很有用,您可能想要投票。如果它满足您的要求,请将其标记为已接受。谢谢
    • 谢谢。我现在就试试。最后一个问题,计算本身是哪一行?数量确实在 B 列 (2) 中。我附上的图片来自旧文件。很抱歉。
    • “计算”行是.Offset(, .Columns.Count).Resize(, 1).FormulaR1C1 = "=SUMIFS(C4, C1,RC1,C3, RC3)" 。具体来说:C4是指第4列(D)要总结的;所有C1 事件都指向第1 列(A)以检查“产品代码”匹配,所有C3 事件都指向第3 列(C)以检查“交货编号”匹配。 -> 将C4 更改为C2
    • 我想我快到了。不幸的是,一件小事不起作用。如果我的范围是 A51:D,我不能写 With .Range("A51:D" &amp; lastrow).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row),而 lastrow 指的是 lastrow = Sheet1.Cells(Sheet1.Rows.Count), "A").End(xlUp).Row'。
    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 2014-05-07
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    相关资源
    最近更新 更多