对未知数量的相同行进行平均和添加数据

时间:2017-07-06 16:12:56

标签: excel vba excel-vba merge average

我的数据基本上就像这样

URL Pageviews Unique Pageviews Avg. Time Entrances Bounce Rate %Exit

AAA 8 1 1.04 8 0.8 0.6 AAA 5 3 0.80 10 0.9 0.6 AAA 10 5 1.11 8 1 0.6 AAA 7 2 0.50 4 0.8 0.6 BB 12 1 0.37 10 0.75 0.6 BB 4 2 0.76 3 0.9 0.6

我想将所有相同的网址合并为一行,并将网页浏览量/唯一网页浏览量/入口列数据相加,并平均平均时间/跳出率/%退出列。

整个表格中相同行数的不同。

这是我到目前为止的代码。我是vba的新手,试图用二维数组来解决这个问题,但是下标超出了范围错误。

Sub Data()
 Dim Ws As Worksheet
 Dim tbl As ListObject
 Dim lastRow As Long, k As Long, t As Long
 Dim arr() As Long
   Set Ws = ThisWorkbook.Worksheets("Data")
   Set tbl = Ws.ListObjects("Table9")

   lastRow = Cells(Rows.Count, "A").End(xlUp).Row


 For t = 1 To lastRow


 arr(0, 1) = Ws.Cells(t, 2).Value
   Do Until Ws.Cells(t, 1).Value <> Ws.Cells(t + 1, 1).Value
        k = k + 1
        t = t + 1
          ReDim Preserve arr(k - 1, 5) As Long
        arr(k - 1, 5) = Ws.Cells(k - 1, 5).Value

   Loop
       Cells(t - k, 2) = Application.WorksheetFunction.Sum(arr)
       Cells(t - k, 3) = Application.WorksheetFunction.Sum(arr)
       Cells(t - k, 4) = Application.WorksheetFunction.Average(dataArray)
       Cells(t - k, 5) = Application.WorksheetFunction.Sum(arr)
       Cells(t - k, 6) = Application.WorksheetFunction.Average(dataArray)
       Cells(t - k, 7) = Application.WorksheetFunction.Average(dataArray)
       Rows(t - k + 1 & k - 1, 5).EntireRow.Delete shift:=xlShiftUp

       t = t - k
       k = 0

  Next t

  Set tbl = Nothing

End Sub

我真的很感激任何帮助,我已经被困了一段时间。

1 个答案:

答案 0 :(得分:0)

好的,我做了一些研究,你正在宣布阵列没问题,但需要将其重新调整为多维尺寸。

(COUNTIFS(DATES,">="&A3,DATES,"<"&EDATE(A3,1),AGENT,"TEST",COMPANY,"TEST",STATUS,OR("Reserved", "Blocked", "Pending", "Archive"))