0% found this document useful (0 votes)
3 views

Methods_DoLoop

The document outlines a VBA program for calculating the Simple Moving Average (SMA) using methods and loops. It includes guidelines for variable passing (ByVal and ByRef) and provides example subroutines for calculating SMA over different data points. Additionally, it suggests completing the program and writing a testing subroutine to compute SMA-5 and SMA-10.

Uploaded by

ming lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Methods_DoLoop

The document outlines a VBA program for calculating the Simple Moving Average (SMA) using methods and loops. It includes guidelines for variable passing (ByVal and ByRef) and provides example subroutines for calculating SMA over different data points. Additionally, it suggests completing the program and writing a testing subroutine to compute SMA-5 and SMA-10.

Uploaded by

ming lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Methods and

Do-loop

1
REVISION
Option Explicit

Sub Testing_Sub()
Debug.print Testing_Calc
Sub DoUntilDemo()
End Sub Do Until IsEmpty(ActiveCell.value)
ActiveCell.value = ActiveCell.value * 2
Function Testing_Calc() As Integer ActiveCell.offset(1, 0).Select
Testing_Calc = 10 Loop
End Function End Sub

• Rules
• If the value of a variable is NOT going to be changed within a method, use ByVal
• If the value of a variable is going to be changed within a method but the value makes NO
IMPACT to the caller method, use ByVal
• If the value of a variable is going to be changed within a method and the value makes
IMPACT to the caller method, use ByRef 2
File:
Methods StockTable.xlsm

Expected to learn:
Sub and Function
• Description: ByRef and ByVal
• Create a macro that utilizes methods for its Do-loop
IsEmpty()
implementation. This macro program is designed Range(col & "1")
to compute the Simple Moving Average (SMA) of
n data points, where n represents any positive ChatGPT
value.
• Utilize ChatGPT to professionally revise the
program.
SMA
Sub CalcSMA_5()
Dim count As Integer
Dim row As Long
Dim sum As Double
Sub CalcSMA_5() Sub CalcSMA_10()
Range("H1").Value = "SMA-5"
row = 2
Dim count As Integer Dim count As Integer
count = 1 Dim row As Long Dim row As Long
sum = 0 Dim sum As Double Dim sum As Double

'Check if Adj close value exists or not Range("H1").Value = "SMA-5" Range("I1").Value = "SMA-10"
Do Until IsEmpty(Cells(row, 7)) row = 2 row = 2
count = 1 count = 1
' accumulate values for calculating SMA-5 sum = 0 sum = 0
sum = sum + Cells(row, 7)
Do Until IsEmpty(Cells(row, 7)) Do Until IsEmpty(Cells(row, 7))
' Check if all 5 data are stored or not
sum = sum + Cells(row, 7) sum = sum + Cells(row, 7)
If count >= 5 Then
If count >= 5 Then If count >= 10 Then
' Write the result to column H Cells(row - 4, "H") = sum / 5 Cells(row - 9, "I") = sum / 10
Cells(row - 4, "H") = sum / 5 sum = sum - Cells(row - 4, 7) sum = sum - Cells(row - 9, 7)
End If End If
' Subtract the first value from the Sum row = row + 1 row = row + 1
sum = sum - Cells(row - 4, 7) count = count + 1 count = count + 1
Loop Loop
End If End Sub End Sub
' Update the row and count values
row = row + 1
count = count + 1
Loop
End Sub
SMA
Sub CalcSMA_5() Sub CalcSMA_10()
Dim count As Integer Dim count As Integer
Dim row As Long Dim row As Long
Dim sum As Double Dim sum As Double

Range("H1").Value = "SMA-5" Range("I1").Value = "SMA-10"


row = 2 row = 2
count = 1 count = 1
sum = 0 sum = 0

Do Until IsEmpty(Cells(row, 7)) Do Until IsEmpty(Cells(row, 7))


sum = sum + Cells(row, 7) sum = sum + Cells(row, 7)
If count >= 5 Then If count >= 10 Then
Cells(row - 4, "H") = sum / 5 Cells(row - 9, "I") = sum / 10
sum = sum - Cells(row - 4, 7) sum = sum - Cells(row - 9, 7)
End If End If
row = row + 1 row = row + 1
count = count + 1 count = count + 1
Loop Loop
End Sub End Sub
SMA Complete this program and write a testing
program to compute SMA-5 and SMA-10.
Sub CalcSMA_5() Sub CalcSMA_10() Sub CalcSMA( , )
Dim count As Integer Dim count As Integer Dim count As Integer
Dim row As Long Dim row As Long Dim row As Long
Dim sum As Double Dim sum As Double Dim sum As Double

Range("H1").Value = "SMA-5" Range("I1").Value = "SMA-10" Range(" 1").Value = "SMA- "


row = 2 row = 2 row = 2
count = 1 count = 1 count = 1
sum = 0 sum = 0 sum = 0

Do Until IsEmpty(Cells(row, 7)) Do Until IsEmpty(Cells(row, 7)) Do Until IsEmpty(Cells(row, 7))


sum = sum + Cells(row, 7) sum = sum + Cells(row, 7) sum = sum + Cells(row, 7)
If count >= 5 Then If count >= 10 Then If count >= Then
Cells(row - 4, "H") = sum / 5 Cells(row - 9, "I") = sum / 10 Cells(row - ( -1) , ) = sum /
sum = sum - Cells(row - 4, 7) sum = sum - Cells(row - 9, 7) sum = sum - Cells(row - ( -1) , 7)
End If End If End If
row = row + 1 row = row + 1 row = row + 1
count = count + 1 count = count + 1 count = count + 1
Loop Loop Loop
End Sub End Sub End Sub
SMA Sub test_SMA()
CalcSMA
CalcSMA
"H", 5
"I", 10
End Sub

Sub CalcSMA(ByVal col As String, ByVal number As Integer)


Calling the method Dim count As Integer
Dim row As Long
Dim sum As Double

Range(col & "1").Value = "SMA-" & number


SMA method row = 2
count = 1
sum = 0

Do Until IsEmpty(Cells(row, 7))


sum = sum + Cells(row, 7)
ChatGPT: If count >= number Then
Cells(row - (number - 1), col) = sum / number
Use For-Each sum = sum - Cells(row - (number - 1), 7)
End If
loop instead of row = row + 1
count = count + 1
Do-loop Loop
End Sub

You might also like