Macro to calculate moving average of dynamically updated cell

rob51852

Board Regular
Joined
Jun 27, 2016
Messages
190
Office Version
  1. 2016
Platform
  1. Windows
Hi,

In cell B2 values are updated dynamically each half second by external software.

I would like to create a simple macro that calculates the average of the previous 10 values and inserts the result in cell C2.

Is this possible?

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this in a COPY of your workbook to see if you can get it working
- for the first test simply use the same cells as I used for everything (to prove the model)
- modify the ranges to suit your preferred worksheet layout after that

B2 is the cell being updated
C2 contains the average
D2:M2 contain the last 10 values

Test Layout.jpg



TEST1
1. Enter Formula below in B1 (this is used to force an event trigger - which may be unnecessary)
=NOW()

2. Place this code in the SHEET code window
- right click on sheet tab\ select view code \ paste the code into that window
VBA Code:
Private Sub Worksheet_Calculate()
    Application.EnableEvents = False
    Range("D2").Resize(, 9).Copy Range("E2")
    Range("B2").Copy Range("D2")
    Range("C2").Formula = "=AVERAGE(D2:M2)"
Debug.Print Time, Range("B2")
    Application.EnableEvents = True
End Sub

3. Test to see if it works

Notes to help you identify any issues etc
Debug.Print writes a value to the Immediate Window in VBA Editor EVERY time the code is triggered
- after testing that line can be removed
- it will help you see how often the event is triggered when B2 is updated
- display the immediate window with {CTRL} g when in VBA editor

Immediate Results.jpg


The code above is triggered every time any cell is amended in the worksheet
- but the value in B2 may not have changed when the cell is amended
- to recalculate the average only when the value in B2 changes then ...
Rich (BB code):
Private Sub Worksheet_Calculate()
    Application.EnableEvents = False
    If Range("B2") <> Range("D2") Then
        Range("D2").Resize(, 9).Copy Range("E2")
        Range("B2").Copy Range("D2")
        Range("C2").Formula = "=AVERAGE(D2:M2)"
    End If
Debug.Print Time, Range("B2")
    Application.EnableEvents = True
End Sub


Let me know how you get on
.
.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top