Arrays 根据列C值对列B求和

Arrays 根据列C值对列B求和,arrays,vba,excel,macros,sumifs,Arrays,Vba,Excel,Macros,Sumifs,我有一个简短的问题:如果第1列和第3列中的值匹配,我会尝试在第2列的4列表中进行汇总。我在这里找到了一个关于堆栈溢出的示例代码,但它当前是基于第1列计算的。我是VBA新手,不知道要更改什么,也不知道如何调整代码以使我的计算基于第1列和第3列。以下是示例代码: Option Explicit Sub testFunction() Dim rng As Excel.Range Dim arrProducts() As String Dim i As Long Set rng =

我有一个简短的问题:如果第1列和第3列中的值匹配,我会尝试在第2列的4列表中进行汇总。我在这里找到了一个关于堆栈溢出的示例代码,但它当前是基于第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

你能告诉我如何解决这个问题吗。下面你可以找到我的小桌子的样本图片。例如,应汇总黄色部分的数量,并删除第二行

您说过“我试图在一个包含4列的表中对第2列进行汇总”,但从您的“示例表-图片”中,我理解您想要对第4列进行汇总

在操作数据范围变化后编辑

假设上述情况,您可以尝试以下方法

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
选项显式
副标题()
错误转到0

使用ActiveSheet“非常感谢@user3598756…”。。。。是的,我指的是根据交货编号和产品代码汇总数量的列(如果两者匹配,则汇总)。。。。我会试试这个谢谢你。。。在E列中(作为下一步),将进行另一个计算。。。。那么您认为辅助列会干扰吗?在E列中(作为下一步),将进行另一个计算。。。。那么您认为辅助列会干扰吗?不,它不会干扰,因为只有在“main”代码运行期间才需要“E”和“F”“helper”列。但我要再次指出,“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开始的,我不能用.range(“A51:D”和lastrow.Resize(.Cells(.Rows.Count,1).End(xlUp.Row)
,而lastrow引用类似于
lastrow=Sheet1.Cells(Sheet1.Rows.Count)
,“A”).End(xlUp.Row”)。