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

Assignment 23 March Codes

Calculation = xlCalculationManual lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row lc = Sheets("BM").Cells(1, Columns.Count).End(xlToLeft).Column Sheets.Add after:=Sheets(Sheets.Count) Sheets(ActiveSheet.Name).Name = "MBM" For Col = 2 To lc For Row = 3 To lr y = Sheets("N").Cells(Row, 1).Value n1 = Sheets("BM").Cells(y, Col).Value Sheets("MBM").Cells(Row - 1, Col).Value = n1 Sheets("

Uploaded by

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

Assignment 23 March Codes

Calculation = xlCalculationManual lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row lc = Sheets("BM").Cells(1, Columns.Count).End(xlToLeft).Column Sheets.Add after:=Sheets(Sheets.Count) Sheets(ActiveSheet.Name).Name = "MBM" For Col = 2 To lc For Row = 3 To lr y = Sheets("N").Cells(Row, 1).Value n1 = Sheets("BM").Cells(y, Col).Value Sheets("MBM").Cells(Row - 1, Col).Value = n1 Sheets("

Uploaded by

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

' Step 1:Delete firms with #ERROR in DataStream

Sub Delete_Errors_Firms()

Dim i As Integer 'Count number of firms (columns)

Dim lc As Integer 'Count lsat column

lc = Sheets("RI").Cells(1, Columns.Count).End(xlToLeft).Column

For i = lc To 2 Step -1

If Sheets("RI").Cells(1, i).Value = "#ERROR" Or Sheets("BM").Cells(1, i).Value = "#ERROR"


Then

Application.DisplayAlerts = False

Sheets("RI").Columns(i).EntireColumn.Delete

Sheets("MV").Columns(i).EntireColumn.Delete

Sheets("P").Columns(i).EntireColumn.Delete

Sheets("VO").Columns(i).EntireColumn.Delete

Sheets("BM").Columns(i).EntireColumn.Delete

Application.DisplayAlerts = True

End If

Next i

End Sub

Sub Nsheet() 'Dayscount in a month

Dim k As Integer

Dim m As Integer

Dim Last As Integer

Dim start As Integer


Application.ScreenUpdating = False

lr = Sheets("RI").Cells(Rows.Count, 1).End(xlUp).Row

Last = 0

start = 0

m=2

For j = 2 To lr

If j <> 2 Then

If (Month(Sheets("RI").Cells(j, 1).Value) <> Month(Sheets("RI").Cells(j - 1, 1).Value)) Then

Sheets("N").Cells(m, 1).Value = Last + 1

Sheets("N").Cells(m, 2).Value = start + 1

m=m+1

End If

End If

Last = Last + 1

start = Last + 1

Next

Application.ScreenUpdating = True

End Sub

Sub Dret()

Dim lr As Integer, lc As Integer, j As Integer, i As Integer, ret As Variant

Application.ScreenUpdating = False

lr = Sheets("RI").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("RI").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "DRL"
Range(Sheets("DRL").Cells(1, 1), Sheets("DRL").Cells(1, lc)).Value = _

Range(Sheets("RI").Cells(1, 1), Sheets("RI").Cells(1, lc)).Value

Range(Sheets("DRL").Cells(1, 1), Sheets("DRL").Cells(lr, 1)).Value = _

Range(Sheets("RI").Cells(1, 1), Sheets("RI").Cells(lr, 1)).Value

Sheets("RI").Activate

For j = 2 To lc

For i = 2 To lr

mat1 = Sheets("RI").Cells(i, j).Value

mat2 = Sheets("RI").Cells(i + 1, j).Value

If mat1 = 0 Or mat1 = "" Or mat2 = "" Then

ret = ""

Else

ret = (mat2 - mat1) / mat1

Sheets("DRL").Cells(i + 1, j).Value = ret

End If

Next

Next

Application.ScreenUpdating = True

End Sub

Sub Daily_Amihud()

Dim lr As Integer

Dim lc As Integer

Dim j As Integer

Dim i As Integer

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

lr = Sheets("P").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("P").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "DAMI"

Sheets("P").Activate

For j = 2 To lr

For i = 2 To lc

mat1 = Sheets("DRL").Cells(j + 1, i).Value

mat2 = Sheets("P").Cells(j + 1, i).Value

mat3 = Sheets("VO").Cells(j + 1, i).Value

If mat1 <> 0 And mat2 <> 0 And mat3 <> 0 And mat1 <> "" And mat2 <> "" And mat3 <> ""
Then

Sheets("DAMI").Cells(j + 1, i).Value = Abs(mat1) / (mat2 * mat3)

End If

Sheets("DAMI").Cells(1, i).Value = Sheets("P").Cells(1, i).Value

Next i

Sheets("DAMI").Cells(j, 1).Value = Sheets("DRL").Cells(j, 1).Value

Next j

Sheets("DAMI").Cells(1, 1).Value = "Date"

Sheets("DAMI").Columns.AutoFit

Application.ScreenUpdating = True

Application.Calculation = xlCalculationAutomatic

End Sub

Sub Lesmond()

Dim Row As Integer, Col As Integer


Application.ScreenUpdating = False

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("DRL").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "LESDE"

For Col = 2 To lc

For Row = 2 To lr

x = Sheets("N").Cells(Row, 2).Value 'this is start of the month

y = Sheets("N").Cells(Row + 1, 1).Value 'this is end of the month

If x And y <> "" Then

Set n = Range(Sheets("DRL").Cells(x, Col), Sheets("DRL").Cells(y, Col))

Count0 = Application.WorksheetFunction.CountIf(n, "=0")

Count = Application.WorksheetFunction.Count(n)

If Count = 0 Or Count - Count0 <= 5 Then ' traded more than 5 days in any month

Sheets("LESDE").Cells(Row, Col).Value = ""

Else

Sheets("LESDE").Cells(Row, Col).Value = Count0 / Count

End If

Sheets("LESDE").Cells(Row, 1).Value = Sheets("DRL").Cells(y, 1).Value

End If

Next Row

Sheets("LESDE").Cells(1, Col).Value = Sheets("DRL").Cells(1, Col).Value

Next Col

Sheets("LESDE").Cells(1, 1).Value = "Date"

Application.ScreenUpdating = True

End Sub
Sub AmihudM()

Dim Row As Integer, Col As Integer

Application.ScreenUpdating = False

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("DAMI").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "AmihudM"

For Col = 2 To lc

For Row = 2 To lr

x = Sheets("N").Cells(Row, 2).Value 'this is start of the month

y = Sheets("N").Cells(Row + 1, 1).Value 'this is end of the month

If x And y <> "" Then

Set n = Range(Sheets("DAMI").Cells(x, Col), Sheets("DAMI").Cells(y, Col))

Sum = Application.WorksheetFunction.Sum(n)

Count = Application.WorksheetFunction.Count(n)

If Count >= 5 Then ' traded more than 5 days in any month

Sheets("AmihudM").Cells(Row, Col).Value = Sum / Count

Else

Sheets("AmihudM").Cells(Row, Col).Value = ""

End If

Sheets("AmihudM").Cells(Row, 1).Value = Sheets("DAMI").Cells(y, 1).Value

End If

Next Row

Sheets("AmihudM").Cells(1, Col).Value = Sheets("DAMI").Cells(1, Col).Value

Next Col

Sheets("AmihudM").Cells(1, 1).Value = "Date"


Application.ScreenUpdating = True

End Sub

Sub MR()

Dim lr As Integer

Dim lc As Integer

Dim k As Integer

Dim m As Integer

Application.ScreenUpdating = False

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("RI").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "Ret"

Range(Sheets("Ret").Cells(1, 1), Sheets("Ret").Cells(1, lc)).Value = _

Range(Sheets("RI").Cells(1, 1), Sheets("RI").Cells(1, lc)).Value

Sheets("Ret").Activate

k=2

For i = 2 To lc

r=2

For m = 2 To lr - 1

x = Sheets("N").Cells(m, 2).Value

y = Sheets("N").Cells(m + 1, 1).Value

P1 = Sheets("RI").Cells(x, k).Value

P2 = Sheets("RI").Cells(y, k).Value

If P1 <> "" And P1 <> 0 And P2 <> "" And P2 <> 0 Then

Sheets("Ret").Cells(r, i).Value = (P2 - P1) / P1


Sheets("Ret").Cells(r, 1).Value = Sheets("RI").Cells(y, 1).Value

Else

Sheets("Ret").Cells(r, i).Value = ""

Sheets("Ret").Cells(r, 1).Value = Sheets("RI").Cells(y, 1).Value

End If

r=r+1

Next

k=k+1

Next

For k = 2 To lc

For g = 2 To lr - 1

x = Sheets("N").Cells(g, 2).Value

y = Sheets("N").Cells(g + 1, 1).Value

If x And y <> "" Then

Set n = Range(Sheets("DRL").Cells(x, k), Sheets("DRL").Cells(y, k))

Count = Application.WorksheetFunction.Count(n)

Count1 = Application.WorksheetFunction.CountIf(n, "=0")

If Count <= 10 Or Count1 >= 10 Then

Sheets("Ret").Cells(g, k).Value = ""

End If

End If

Next

Next

Application.ScreenUpdating = True

End Sub

Sub MMV()
Dim lr As Integer

Dim lc As Integer

Dim c As Integer

Dim r As Integer

Application.ScreenUpdating = False

On Error Resume Next

Application.DisplayAlerts = False

Sheets("MMV").Delete

Application.DisplayAlerts = True

Application.Calculation = xlCalculationManual

Application.Calculation = xlCalculationManual

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("MV").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "MMV"

For Col = 2 To lc

For Row = 3 To lr

y = Sheets("N").Cells(Row, 1).Value

n1 = Sheets("MV").Cells(y, Col).Value

Sheets("MMV").Cells(Row - 1, Col).Value = n1

Sheets("MMV").Cells(Row - 1, 1).Value = Sheets("MV").Cells(y, 1).Value

Next Row

Sheets("MMV").Cells(1, Col).Value = Sheets("MV").Cells(1, Col).Value

Next Col

Sheets("MMV").Cells(1, 1).Value = "Date"

Application.ScreenUpdating = True
End Sub

Sub MVO()

Dim lr As Integer

Dim lc As Integer

Dim c As Integer

Dim r As Integer

Application.ScreenUpdating = False

On Error Resume Next

Application.DisplayAlerts = False

Sheets("MVO").Delete

Application.DisplayAlerts = True

Application.Calculation = xlCalculationManual

Application.Calculation = xlCalculationManual

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("VO").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "MVO"

For Col = 2 To lc

For Row = 3 To lr

y = Sheets("N").Cells(Row, 1).Value

n1 = Sheets("VO").Cells(y, Col).Value

Sheets("MVO").Cells(Row - 1, Col).Value = n1

Sheets("MVO").Cells(Row - 1, 1).Value = Sheets("VO").Cells(y, 1).Value

Next Row
Sheets("MVO").Cells(1, Col).Value = Sheets("VO").Cells(1, Col).Value

Next Col

Sheets("MVO").Cells(1, 1).Value = "Date"

Application.ScreenUpdating = True

End Sub

Sub MBM()

Dim lr As Integer

Dim lc As Integer

Dim c As Integer

Dim r As Integer

Application.ScreenUpdating = False

On Error Resume Next

Application.Calculation = xlCalculationManual

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("BM").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "MBM"

For Col = 2 To lc

For Row = 3 To lr

y = Sheets("N").Cells(Row, 1).Value

n1 = Sheets("BM").Cells(y, Col).Value

Sheets("MBM").Cells(Row - 1, Col).Value = n1

Sheets("MBM").Cells(Row - 1, 1).Value = Sheets("BM").Cells(y, 1).Value

Next Row

Sheets("MBM").Cells(1, Col).Value = Sheets("BM").Cells(1, Col).Value


Next Col

Sheets("MBM").Cells(1, 1).Value = "Date"

Application.Calculation = xlCalculationAutomatic

Application.ScreenUpdating = True

End Sub

Sub DZ() 'Delete Zero Returns in a month

Dim lr As Integer

Dim lc As Integer

Dim c As Integer

Dim r As Integer

Application.ScreenUpdating = False

Application.Calculation = xlCalculationManual

lc = Sheets("DRL").Cells(1, Columns.Count).End(xlToLeft).Column

lr = Sheets("DRL").Cells(Rows.Count, 1).End(xlUp).Row

Sheets("DRL").Activate

k=2

For c = 2 To lc

m=2

For r = 2 To lr

x = Sheets("N").Cells(m, 2).Value

y = Sheets("N").Cells(m + 1, 1).Value

If x And y <> "" Then

Set n = Range(Sheets("DRL").Cells(x, k), Sheets("DRL").Cells(y, k))

Count = Application.WorksheetFunction.CountIf(n, "<>0")

If Count <= 10 Then

Range(Sheets("DRL").Cells(x, k), Sheets("DRL").Cells(y, k)) = ""


End If

End If

m=m+1

Next r

k=k+1

Next c

Application.ScreenUpdating = True

Application.Calculation = xlCalculationAutomatic

End Sub

Sub Panel_Corr()

Dim lr As Integer

Dim c As Integer

Dim g As Integer

Dim ID As Integer

Dim x As Variant

Dim D As Date

Application.ScreenUpdating = False

lr = Sheets("Ret").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("Ret").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "Panel"
x=2

For g = 2 To lr ' essential to take each row into the columns

ID = 1 'firms are given unique number as its position in sample

For c = 2 To lc ' Take all necessary information across firms

D = Sheets("Ret").Cells(g, 1).Value ' taking dates

Ret_pak = Sheets("Ret").Cells(g, c).Value 'taking returns

Illiq_pk = Sheets("AmihudM").Cells(g, c).Value ' taking illiquidity

MV_pak = Sheets("MMV").Cells(g, c).Value ' taking market capitalization

MVO_pak = Sheets("MVO").Cells(g, c).Value

BM_pak = Sheets("BM").Cells(g, c).Value 'taking BTM

Lesm_pak = Sheets("LESDE").Cells(g, c).Value 'Taking Betas

If Ret_pak <> "" And Illiq_pk <> "" And MV_pak <> "" And MVO_pak And BM_pak <> "" _

And Lesm_pak <> "" Then ' take common firms that have all data

Sheets("Panel").Cells(x, 1).Value = D

Sheets("Panel").Cells(x, 2).Value = ID

Sheets("Panel").Cells(x, 3).Value = Ret_pak

Sheets("Panel").Cells(x, 4).Value = Illiq_pk

Sheets("Panel").Cells(x, 5).Value = MV_pak

Sheets("Panel").Cells(x, 6).Value = MVO_pak

Sheets("Panel").Cells(x, 7).Value = BM_pak

Sheets("Panel").Cells(x, 8).Value = Lesm_pak

x = x + 1 ' It will when all columns are exhausted

End If

ID = ID + 1
Next c

Next g 'repeat each month the same thing

Sheets("Panel").Cells(1, 1).Value = "Date"

Sheets("Panel").Cells(1, 2).Value = "ID"

Sheets("Panel").Cells(1, 3).Value = "Returns"

Sheets("Panel").Cells(1, 4).Value = "Amihud"

Sheets("Panel").Cells(1, 5).Value = "Size"

Sheets("Panel").Cells(1, 6).Value = "Volume"

Sheets("Panel").Cells(1, 7).Value = "BTPV"

Sheets("Panel").Cells(1, 8).Value = "Lesmond"

End Sub

Sub LMBM()

Dim lr As Integer

Dim lc As Integer

Dim c As Integer

Dim r As Integer

Application.ScreenUpdating = False

On Error Resume Next

Application.Calculation = xlCalculationManual

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("BM").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)
Sheets(ActiveSheet.Name).Name = "LMBM"

For Col = 2 To lc

For Row = 3 To lr

y = Sheets("N").Cells(Row, 1).Value

n1 = Sheets("BM").Cells(y, Col).Value

Sheets("LMBM").Cells(Row - 1, Col).Value = Application.WorksheetFunction.Ln(n1)

Sheets("LMBM").Cells(Row - 1, 1).Value = Sheets("BM").Cells(y, 1).Value

Next Row

Sheets("LMBM").Cells(1, Col).Value = Sheets("BM").Cells(1, Col).Value

Next Col

Sheets("LMBM").Cells(1, 1).Value = "Date"

Application.Calculation = xlCalculationAutomatic

Application.ScreenUpdating = True

End Sub

Sub LMMV()

Dim lr As Integer

Dim lc As Integer

Dim c As Integer

Dim r As Integer

Application.ScreenUpdating = False

On Error Resume Next

Application.Calculation = xlCalculationManual

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("MV").Cells(1, Columns.Count).End(xlToLeft).Column
Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "LMMV"

For Col = 2 To lc

For Row = 3 To lr

y = Sheets("N").Cells(Row, 1).Value

n1 = Sheets("MV").Cells(y, Col).Value

Sheets("LMMV").Cells(Row - 1, Col).Value = Application.WorksheetFunction.Ln(n1)

Sheets("LMMV").Cells(Row - 1, 1).Value = Sheets("MV").Cells(y, 1).Value

Next Row

Sheets("LMMV").Cells(1, Col).Value = Sheets("MV").Cells(1, Col).Value

Next Col

Sheets("LMMV").Cells(1, 1).Value = "Date"

Application.Calculation = xlCalculationAutomatic

Application.ScreenUpdating = True

End Sub

'Step 11: Eliminate Thinly Traded Firms

Sub Comp1() 'Compatable MR with Lesmond3

Dim lr As Integer

Dim lc As Integer

Dim i As Integer
Dim j As Integer

Application.ScreenUpdating = False

Application.Calculation = xlCalculationManual

lr = Sheets("Ret").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("Ret").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "MRC"

Sheets("LESDE").Activate

For i = 2 To lc

For j = 2 To lr

If (Sheets("LESDE").Cells(j, i).Value) <> "" Then

Sheets("MRC").Cells(j, i).Value = Sheets("Ret").Cells(j, i).Value

Else

Sheets("MRC").Cells(j, i).Value = ""

End If

Sheets("MRC").Cells(j, 1).Value = Sheets("Ret").Cells(j, 1).Value

Next j

Sheets("MRC").Cells(1, i).Value = Sheets("Ret").Cells(1, i).Value

Next i

Sheets("MRC").Cells(1, 1).Value = "Date"

Sheets("MRC").Columns.AutoFit

Application.ScreenUpdating = True

Application.Calculation = xlCalculationAutomatic

End Sub

Sub fama()
Dim lr As Long, lc As Long, r As Integer, c As Integer

Application.ScreenUpdating = False

On Error Resume Next

lr = Sheets("MRC").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("MRC").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "FF3"

For r = 2 To lr - 2

Set n = Range(Sheets("LMMV").Cells(r, 2), Sheets("LMMV").Cells(r, lc))

Set m = Range(Sheets("LMBM").Cells(r, 2), Sheets("LMBM").Cells(r, lc))

Set returns = Range(Sheets("MRC").Cells(r + 2, 2), Sheets("MRC").Cells(r + 2, lc))

P1 = Application.WorksheetFunction.Percentile(n, 0.5)

P2 = Application.WorksheetFunction.Percentile(m, 0.3)

P3 = Application.WorksheetFunction.Percentile(m, 0.7)

Sheets("FF3").Cells(r, 2).Value = Application.WorksheetFunction.AverageIfs(returns, n, "<=" & P1, m,


"<=" & P2)

Sheets("FF3").Cells(r, 3).Value = Application.WorksheetFunction.AverageIfs(returns, n, "<=" & P1, m,


">" & P2, m, "<=" & P3)

Sheets("FF3").Cells(r, 4).Value = Application.WorksheetFunction.AverageIfs(returns, n, "<=" & P1, m,


">" & P3)

Sheets("FF3").Cells(r, 5).Value = Application.WorksheetFunction.AverageIfs(returns, n, ">" & P1, m,


"<=" & P2)

Sheets("FF3").Cells(r, 6).Value = Application.WorksheetFunction.AverageIfs(returns, n, ">" & P1, m,


">" & P2, m, "<=" & P3)
Sheets("FF3").Cells(r, 7).Value = Application.WorksheetFunction.AverageIfs(returns, n, ">" & P1, m,
">" & P3)

Sheets("FF3").Cells(r, 8).Value = Application.WorksheetFunction.Average(returns)

Sheets("FF3").Cells(r, 9).Value = ((Sheets("FF3").Cells(r, 2).Value + Sheets("FF3").Cells(r, 3).Value +


Sheets("FF3").Cells(r, 4).Value) / 3) _

- (Sheets("FF3").Cells(r, 5).Value + Sheets("FF3").Cells(r, 6).Value + Sheets("FF3").Cells(r, 7).Value) / 3

Sheets("FF3").Cells(r, 10).Value = ((Sheets("FF3").Cells(r, 4).Value + Sheets("FF3").Cells(r, 7).Value) /


2) _

- (Sheets("FF3").Cells(r, 2).Value + Sheets("FF3").Cells(r, 5).Value) / 2

Sheets("FF3").Cells(r, 1).Value = Sheets("MRC").Cells(r + 2, 1).Value

Next r

Sheets("FF3").Cells(1, 1).Value = "Date"

Sheets("FF3").Cells(1, 2).Value = "SL"

Sheets("FF3").Cells(1, 3).Value = "SM"

Sheets("FF3").Cells(1, 4).Value = "SH"

Sheets("FF3").Cells(1, 5).Value = "BL"

Sheets("FF3").Cells(1, 6).Value = "BM"

Sheets("FF3").Cells(1, 7).Value = "BH"

Sheets("FF3").Cells(1, 8).Value = "MKT"

Sheets("FF3").Cells(1, 9).Value = "SMB"

Sheets("FF3").Cells(1, 10).Value = "HML"

End Sub
Sub Amihud12()

Dim Row As Integer, Col As Integer

Application.ScreenUpdating = False

lr = Sheets("N").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("DAMI").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "Amihud"

For i = 1 To 12

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "AmihudM" & i

For Col = 2 To lc

x=0

y=0

For Row = 2 To lr

x = Sheets("N").Cells(Row, 2).Value 'this is start of the month

y = Sheets("N").Cells(Row + i, 1).Value 'this is end of the month

If x And y <> "" Then

Set n = Range(Sheets("DAMI").Cells(x, Col), Sheets("DAMI").Cells(y, Col))

Sum = Application.WorksheetFunction.Sum(n)

Count = Application.WorksheetFunction.Count(n)

If Count >= 5 Then ' traded more than 5 days in any month

Sheets("AmihudM" & i).Cells(Row + i, Col).Value = Sum / Count

Else

Sheets("AmihudM" & i).Cells(Row + i, Col).Value = ""

End If

Sheets("AmihudM" & i).Cells(Row + i, 1).Value = Sheets("DAMI").Cells(y, 1).Value

End If
Next Row

Sheets("AmihudM" & i).Cells(1, Col).Value = Sheets("DAMI").Cells(1, Col).Value

Next Col

Sheets("AmihudM" & i).Cells(1, 1).Value = "Date"

Next

Application.ScreenUpdating = True

End Sub

Sub MPBM()

Dim lr As Long, lc As Long, r As Integer, c As Integer

Application.ScreenUpdating = False

On Error Resume Next

lr = Sheets("AmihudM12").Cells(Rows.Count, 1).End(xlUp).Row

lc = Sheets("AmihudM12").Cells(1, Columns.Count).End(xlToLeft).Column

Sheets.Add after:=Sheets(Sheets.Count)

Sheets(ActiveSheet.Name).Name = "Ami12ret"

x=2

For r = 14 To lr

Set n = Range(Sheets("AmihudM12").Cells(r, 2), Sheets("AmihudM12").Cells(r, lc))

Set m = Range(Sheets("MRC").Cells(r + 2, 2), Sheets("MRC").Cells(r + 2, lc))

P1 = Application.WorksheetFunction.Percentile(n, 0.25)

P2 = Application.WorksheetFunction.Percentile(n, 0.5)

P3 = Application.WorksheetFunction.Percentile(n, 0.75)
Sheets("Ami12ret").Cells(x, 2).Value = Application.WorksheetFunction.AverageIfs(m, n, "<=" & P1)

Sheets("Ami12ret").Cells(x, 3).Value = Application.WorksheetFunction.AverageIfs(m, n, ">" & P1, n,


"<=" & P2)

Sheets("Ami12ret").Cells(x, 4).Value = Application.WorksheetFunction.AverageIfs(m, n, ">" & P2, n,


"<=" & P3)

Sheets("Ami12ret").Cells(x, 5).Value = Application.WorksheetFunction.AverageIfs(m, n, ">" & P3)

Sheets("Ami12ret").Cells(x, 6).Value = Sheets("Ami12ret").Cells(x, 5).Value -


Sheets("Ami12ret").Cells(x, 2).Value

Sheets("Ami12ret").Cells(x, 1).Value = Sheets("MRC").Cells(r + 2, 1).Value

x=x+1

Next r

Sheets("Ami12ret").Cells(1, 1).Value = "Date"

Sheets("Ami12ret").Cells(1, 2).Value = "Liq1"

Sheets("Ami12ret").Cells(1, 3).Value = "Liq2"

Sheets("Ami12ret").Cells(1, 4).Value = "Liq3"

Sheets("Ami12ret").Cells(1, 5).Value = "Liq4"

Sheets("Ami12ret").Cells(1, 6).Value = "LP"

End Sub

You might also like