Exponential moving average using array in Excel VBA

Super P

New Member
Joined
May 22, 2021
Messages
28
Office Version
  1. 365
Platform
  1. Windows
The array VBA code below calculates 9 periods exponential moving average of the values in Column U (from row 31 to last row ~row 6983). The output will be in Column V (from row 39 to last row). he problem now the second output value is 39 rows below i.e. cell V78, this should be located in cell V40, appreciate any VBA expert help, thanks

VBA Code:
Option Explicit

Sub EMA9()
   
    Dim valArray As Variant
    Dim runSum, EMA9() As Double
    Dim i, lastRow, lRow, firstRow, x1, x2, iPeriod, iCol As Long

    'Step1 - set last row and reference range to calculate
    With Worksheets("Sheet1")
      lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
      valArray = .Range(.Cells(1, 21), .Cells(lastRow, 21)).Value2
    End With
   
    'Step2 - set lower and upper limit
    iPeriod = 9
    x1 = 2 / (iPeriod + 1)
    x2 = 1 - (2 / (iPeriod + 1))
    ReDim EMA9(LBound(valArray, 1) - 38 To UBound(valArray, 1), 1 To 1)
   
    'Step3 - calculate first row value, [sum (row 31 to row 39)]
    firstRow = 31
    runSum = 0
    For i = firstRow To (iPeriod + 30)
      runSum = runSum + valArray(i, 1)
    Next
   
    'Step4 - calculate first row average value, [average (row 31 to row 39)]
    EMA9(1, 1) = runSum / iPeriod
   
    'Step5 - calculate the 2nd row (row 32) value onwards, [current value of valArray * x1 + previous value of EMA9 * x2]
    For i = (iPeriod + firstRow) To UBound(valArray, 1)
        EMA9(i, 1) = valArray(i, 1) * x1 + EMA9(i - 1, 1) * x2
    Next
   
    'Step6 - write the values to worksheet
    iCol = 22
    With Worksheets("Sheet1")
        .Range(.Cells((iPeriod - 8), iCol), .Cells(lastRow, iCol)).Value2 = EMA9
    End With
   
    'Step7 - clear memory
    Erase valArray: Erase EMA9

End Sub

1648379455985.png
 
Last edited by a moderator:
in your 365, perhaps this one is easier to understand
VBA Code:
    MyRows = WorksheetFunction.Sequence(6000 - 39 + 1, 1, 39)  '2D-array starting with 39 until 6000
you can also write immediately 5962 instead of that formula.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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