excel 将订单合并到单行,计算订单数并求结果的平均值

gcuhipw9  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(168)

每一行是一个单独的订单的一部分。我需要计算有多少每个订单,这样我就可以计算平均部分订购这是每个订单的数量。我有许多行,并希望公式或vba的建议,如何自动化这一点。
一旦我得到了订单数并计算了每个订单的平均部件数,我就必须在一行中显示结果,消除所有单独的订单行,因为我已经填写了每个订单的平均部件数。
我正在寻找实现这一目标的最佳方法的方向。感谢您的时间和考虑。
这是一个WIP,但在这个阶段,我不知道VBA是否是完成我所需要的唯一方法。我试图建立一个基于零件名称相同的范围。一对夫妇的问题是,当零件名称与当前单元格值不相同时,它将跳过该单元格之前,代码纠正创建孔的问题,也一旦我建立了范围,我不'I don“我不知道如何只求范围内第三列的平均值。

Sub aveCount()
    
    Dim rng As Range
    Dim cl As Range
    Dim partName As String
    Dim startAddress As String
    Dim ws As Worksheet
    Dim count As Double
    Dim orders As Double
    Dim i As Integer
    
        Set ws = ActiveWorkbook.Worksheets("Sheet1")
        'lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Application.ScreenUpdating = False
        i = 0
        For Each cl In ws.Range("A89:A433")
            If i = 0 Then
                partName = cl.Value
            End If
            
            If cl.Value = partName Then
                i = i + 1
                
                If rng Is Nothing Then
                    startAddress = cl.Address
                    Set rng = ws.Range(cl.Address).Resize(, 4)
                Else
                    Set rng = Union(rng, ws.Range(cl.Address).Resize(, 4))
                End If
            Else
                i = 0
            End If
            count = rng.Rows.count
            ws.Range(startAddress).Offset(0, 4) = Application.WorksheetFunction.Subtotal(1, rng)
            Debug.Print (startAddress)
            Stop
     
        Next cl 'next row essentially
    
    End Sub
zpgglvta

zpgglvta1#

如果两个订单上没有相同的零件......这将为您提供平均值:

vof42yt1

vof42yt12#

Sub aveCount()

Dim rng As Range
Dim cl As Range
Dim partName As String
Dim startAddress As String
Dim ws As Worksheet
Dim count As Double
Dim orders As Double
Dim i As Integer

    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    'lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Application.ScreenUpdating = False
    'initializing the variable
    startAddress = ws.Range("A141").Address
    
    i = 1
    For Each cl In ws.Range(startAddress & ":A433")
        If cl.Value = cl.Offset(1, 0).Value Then
            i = i + 1
            Debug.Print (i)
            Debug.Print (cl.Address)
            If rng Is Nothing Then
                Set rng = ws.Range(cl.Address).Resize(, 4)
                orders = cl.Offset(0, 2).Value
            Else
                Set rng = Union(rng, ws.Range(cl.Address).Resize(, 4))
                orders = orders + cl.Offset(0, 2).Value
            End If
            Debug.Print (orders)
        Else
            orders = orders + cl.Offset(0, 2).Value
            Debug.Print (cl.Address)
            Debug.Print (orders)
            ws.Range(startAddress).Offset(0, 3) = i
            ws.Range(startAddress).Offset(0, 4) = orders / i
            startAddress = ws.Range(startAddress).Offset(i, 0).Address
            i = 1
        End If
        
    Next cl 'next row essentially
    
End Sub

相关问题