VBA code to calculate monthly variance

Sumaira

New Member
Joined
Dec 7, 2020
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I have a daily data of stock returns of18 years in one sheet. For example my data starts with 7/1/2010 and ends at 6/30/2020. I have to calculate monthly variance of this data Means variance of june 2010 by using all observations of June 2010 then variance of august 2010 and so on till june 2020. can someone help in making a VBA code for this
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Salam and welcome to the board,

I would suggest uploading your sample data using the xl2bb addon, that way it will be much easier to understand and suggest solutions.
 
Upvote 0
Thank you so much for responding. i would greatly appreciate your help.

book.xlsx
CDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
37/1/20107/2/20107/5/20107/6/20107/7/20107/8/20107/9/20107/12/20107/13/20107/14/20107/15/20107/16/20107/19/20107/20/20107/21/20107/22/20107/23/20107/26/20107/27/20107/28/20107/29/20107/30/20108/2/20108/3/20108/4/20108/5/20108/6/20108/9/20108/10/20108/11/20108/12/2010
4NESTLE PAKISTAN - TOT RETURN IND0.010.00-0.04-0.040.050.040.01-0.010.020.02-0.01-0.020.030.00-0.01-0.010.020.000.00-0.010.000.00-0.010.01-0.010.00-0.010.04-0.040.01-0.03
5OIL & GAS DEVELOPMENT COMPANY - TOT RETURN IND0.000.00-0.01-0.020.020.000.020.010.03-0.01-0.010.010.000.000.000.000.020.010.000.000.000.00-0.020.000.000.010.00-0.03-0.03-0.01-0.02
6PAKISTAN TOBACCO COMPANY - TOT RETURN IND0.00-0.020.010.050.00-0.030.000.000.000.03-0.030.030.01-0.030.000.03-0.020.010.000.000.010.02-0.040.000.000.03-0.020.020.02-0.040.00
7ABBOTT LABS.(PAK.) - TOT RETURN IND-0.010.00-0.040.000.030.000.010.000.010.030.000.000.01-0.010.02-0.030.01-0.010.010.010.000.01-0.010.000.010.010.00-0.030.000.000.00
8ABDULLAH SHAH GAZI SUGAR MILLS - TOT RETURN IND0.00-0.22-0.140.00-0.270.000.001.240.00-0.150.00-0.29-0.39-0.150.890.21-0.370.000.380.44-0.290.430.300.230.13-0.02-0.170.10-0.130.05-0.05
9ABSON INDUSTRIES SUSP - SUSP.28/01/16 - TOT RETURN IND0.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.00
10ADAM SUGAR MILLS - TOT RETURN IND0.000.070.000.000.000.000.070.000.000.000.000.000.000.000.000.03-0.020.000.000.000.000.000.00-0.070.000.080.000.000.00-0.050.00
11ADOS PAKISTAN - TOT RETURN IND0.000.040.010.030.00-0.050.000.030.010.030.000.03-0.050.04-0.040.040.00-0.050.000.040.010.01-0.040.01-0.020.000.00-0.020.000.000.03
12#ERRORNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
13AGRIAUTO INDUSTRIES - TOT RETURN IND0.000.000.00-0.050.010.000.010.050.020.01-0.030.040.02-0.020.010.010.000.00-0.01-0.030.030.02-0.030.02-0.020.00-0.01-0.030.000.00-0.01
14#ERROR-0.050.05-0.040.040.030.01-0.010.030.00-0.010.010.010.000.01-0.010.00-0.020.01-0.030.02-0.030.000.00-0.010.01-0.030.00-0.04-0.020.02-0.02
Sheet1
Cell Formulas
RangeFormula
D4:AH14D4=IFERROR([PAKISTAN.xlsx]Sheet2!CBL4-D$2,"NaN")
 
Upvote 0
a question, what do you mean by ??
variance of june 2010 by using all observations of June 2010 then variance of august 2010 and so on till june
does this mean you need MEAN for each month? if so, where do you need this variance, i mean in this same worksheet or another worksheet?
 
Upvote 0
Basically I want to ask if there is a way to put a loop command. Like I have data of 120 months how does a single command be executed to all these months one by one.
 
Upvote 0
hi,
Create a results sheet (sheet 3) with similar structure to your data sheet (sheet 2), similar to below sample:
Book1.xlsm
ABCDEFGHIJKLM
12010
2JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
3NESTLE PAKISTAN - TOT RETURN IND
4OIL & GAS DEVELOPMENT COMPANY - TOT RETURN IND
5PAKISTAN TOBACCO COMPANY - TOT RETURN IND
6ABBOTT LABS.(PAK.) - TOT RETURN IND
7ABDULLAH SHAH GAZI SUGAR MILLS - TOT RETURN IND
8ABSON INDUSTRIES SUSP - SUSP.28/01/16 - TOT RETURN IND
9ADAM SUGAR MILLS - TOT RETURN IND
10ADOS PAKISTAN - TOT RETURN IND
11#ERROR
12AGRIAUTO INDUSTRIES - TOT RETURN IND
13#ERROR
Sheet3


the year you are looking for goes in Cell B2 (2010,2011,2012,..... etc).

Copy and paste the code below in a module and bind it to a button on your results sheet.

VBA Code:
Private Sub sumss()
Dim srng As Range
Dim crng As Range
Dim cmonth As String

lr = Cells(Rows.Count, 1).End(xlUp).Row
lc = Cells(2, Columns.Count).End(xlToLeft).Column

With Sheets("sheet3")

    For j = 3 To lr
        For i = 2 To lc
            Set srng = Sheets("sheet2").Range("b" & j, "af" & j)
            Set crng = Sheets("sheet2").Range("b2:af2")
            cmonth = DateValue("01/" & Cells(2, i).Value & "/" & Cells(1, 2).Value)
On Error Resume Next
            Cells(j, i) = Application.WorksheetFunction.AverageIfs(srng, crng, ">=" & cmonth, _
                                crng, "<=" & Application.WorksheetFunction.EoMonth(cmonth, 0))
            Cells(j, i).NumberFormat = "#,###0.00000"
        Next i
    Next j

End With
End Sub

Basically the structure of both your sheets will remain the same for the code to work properly.
1607496005090.png


hope this helps.

P.S. you can choose to change sheet names and ranges to suit your needs.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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