Forum Discussion

Rory123908's avatar
Rory123908
Copper Contributor
Nov 30, 2021

Averaging data every 2 cells using VBA

Hi there, I have 24 columns of data, each with 86400 thousands rows of data. I am l looking to average the data so that it is represented by 43200 rows. Therefore, I am looking to go down each column, average 2 cells, then average the next 2, then the next 2, and so on. I am trying to use a macro for this but so far I have been unable to solve the problem. I was wondering if anyone here could help me? It would be much appreciated. Thanks in advance for all your help!!

6 Replies

  • Rory123908 

    Try this:

    Sub Average2()
        Const fr = 2 ' First data row - change as needed
        Dim ws As Worksheet
        Dim wt As Worksheet
        Application.ScreenUpdating = False
        Set ws = ActiveSheet
        Set wt = Worksheets.Add(After:=ws)
        With wt.Range("A" & fr).Resize(43200, 24)
            .Formula = "=AVERAGE(OFFSET('" & ws.Name & "'!A$" & fr & ",2*(ROW()-" & fr & "),0,2,1))"
            .Value = .Value
        End With
        Application.ScreenUpdating = True
    End Sub
    • Rory123908's avatar
      Rory123908
      Copper Contributor
      Hi there, Many thanks for your help. The solution worked! I really appreciate you helping me out. My data is now averaged inside of a new sheet! The data started on row 2 so implementing it was easy! How would I change the code so that I am able to copy the titles/headers from row 1? (these obviously dont need to be averaged or anything like this, just simply copied over). Additionally, There is another column in the sheet that I would also like to copy across to this new sheet. This also does not need any operations done on it, simply copied along side the averaged data. Do you know how I may do this? Thanks again for all your help, it really has been very helpful!
      • HansVogelaar's avatar
        HansVogelaar
        MVP

        Rory123908 

        Which column should be copied?

        And do you want to copy the value from the even rows or from the odd rows in that column?

Resources