Weighted Moving Average Calculation in VBA

artz

Well-known Member
Joined
Aug 11, 2002
Messages
830
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am trying to perform a Weighted Moving Average Calculation in VBA. The code below seems simple enough but generates an error. Input data are in column B starting at row 2. WMA output is in column G starting at row 2.

Is there someone in the Forum who can determine what is causing the error? Here's the code:
Code:
Sub wma()

Dim i As Long
Dim n As Long
Dim wma As Double
Dim weight As Double
Dim sumWeight As Double
Dim colData As Long
Dim colWMA As Long

colData = 2 ' Column containing the data
colWMA = 7 ' Column to output the WMA
n = 5 ' Length of the WMA

 For i = 2 To Cells(Rows.Count, colData).End(xlUp).Row
    wma = 0
    weight = 0
    sumWeight = 0
    For j = i - n + 1 To i
        weight = weight + 1
       wma = wma + Cells(j, colData) * weight
    sumWeight = sumWeight + weight
    sumWeight = 1
         Next j
    Cells(i, colWMA).Value = wma / sumWeight
Next i
 
End Sub

Any help would be greatly appreciated.

Thanks,

Art
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Look at your first iteration in your loop.

n = 5
i = 2

So this line:
VBA Code:
For j = i - n + 1 To i
will translate to:
VBA Code:
For j = 2 - 5 + 1 To 2
which simplifies to:
VBA Code:
For j = -2 To 2
so the first value for j is -2.

However, you are trying to use i as a row number here:
VBA Code:
wma = wma + Cells(j, colData) * weight
It is impossible to have a negative row number!
Row -2 makes no sense, which is why you are getting error.

That is the best way of debugging loops like this. Work through the details and figure out what the values are at the time.
If you use F8, you can step through your code line-by-line, one line at a time, and if you hover over any variable, it will show you the exact value of that variable at that point in time.
 
Upvote 0
Look at your first iteration in your loop.

n = 5
i = 2

So this line:
VBA Code:
For j = i - n + 1 To i
will translate to:
VBA Code:
For j = 2 - 5 + 1 To 2
which simplifies to:
VBA Code:
For j = -2 To 2
so the first value for j is -2.

However, you are trying to use i as a row number here:
VBA Code:
wma = wma + Cells(j, colData) * weight
It is impossible to have a negative row number!
Row -2 makes no sense, which is why you are getting error.

That is the best way of debugging loops like this. Work through the details and figure out what the values are at the time.
If you use F8, you can step through your code line-by-line, one line at a time, and if you hover over any variable, it will show you the exact value of that variable at that point in time.
 
Upvote 0
Hi Joe4,
Thanks for your response and comments. I did step through the code using F8 and the variable values look incorrect. I did not originate this code so I am not sure what I need to do to correct the issues.
Do you have some modifications that would correct the problem with the loops? I understand that there can't be a negative row number.
Thanks,
Art
 
Upvote 0
Hi Joe4,
Thanks for your response and comments. I did step through the code using F8 and the variable values look incorrect. I did not originate this code so I am not sure what I need to do to correct the issues.
Do you have some modifications that would correct the problem with the loops? I understand that there can't be a negative row number.
Thanks,
Art
Without seeing your data, and having a full understanding of how you want it to interact with that particular data structure, I cannot say for sure.

Note that if you can create the formula you need in Excel (using Excel workbook formulas), you can usually use most of those formulas in VBA by using "Application.WorksheetFunction..." or "Evaluate".
 
Upvote 0
Here's a screen cap of the worksheet.
 

Attachments

  • MA_Data1.jpg
    MA_Data1.jpg
    165 KB · Views: 11
Upvote 0

Forum statistics

Threads
1,216,041
Messages
6,128,467
Members
449,455
Latest member
jesski

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