-
Notifications
You must be signed in to change notification settings - Fork 1
/
VBA code for function of pension calculation
369 lines (139 loc) · 8.17 KB
/
VBA code for function of pension calculation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
Function cum_gp(G As String, male_q As Variant, female_q As Variant, age As Integer, year As Double, t As Integer)
Dim I As Integer
cum_gp = 1
If t > 0 Then
For I = age To age + t - 1
cum_gp = cum_gp * (1 - gen_p(G, male_q, female_q, I, year + (I - age)))
Next I
End If
End Function
Function cum_totalP(G As String, male_q As Variant, female_q As Variant, r_q As Variant, t_q As Variant, age As Integer, year As Double, t As Integer)
Dim I As Integer
cum_totalP = 1
If t > 0 Then
For I = age To age + t - 1
cum_totalP = cum_totalP * totalP(G, male_q, female_q, r_q, t_q, I, year + (I - age))
Next I
End If
End Function
Function totalP(G As String, male_q As Variant, female_q As Variant, r_q As Variant, t_q As Variant, age As Integer, year As Double)
totalP = 1 - retire_q(r_q, age) - terminate_q(t_q, age) - gen_p(G, male_q, female_q, age, year)
If totalP < 0 Then
totalP = 0
End If
End Function
Function gen_p(G As String, male_q As Variant, female_q As Variant, age As Integer, year As Double)
If G = "M" Then
gen_p = male_q(age - 20 + 2, year - 2014 + 2)
Else
gen_p = female_q(age - 20 + 2, year - 2014 + 2)
End If
End Function
Function retire_q(r_q As Variant, age As Integer)
retire_q = r_q(age - 20 + 2)
End Function
Function terminate_q(t_q As Variant, age As Integer)
terminate_q = t_q(age - 20 + 2)
End Function
Function EarlyRetireF(I As Integer)
If I >= 55 And I < 65 Then
EarlyRetireF = (0.5 + 0.05 * (I - 55))
ElseIf I >= 65 Then
EarlyRetireF = 1
End If
End Function
Function AnnualPay(OriginalSalary As Double, I As Integer, age As Integer)
AnnualPay = OriginalSalary
AnnualPay = AnnualPay * (1 + 0.025) ^ (I - age)
End Function
Function ServiceYear(OriginalServiceYear As Integer, I As Integer, age As Integer)
ServiceYear = OriginalServiceYear + I - age
End Function
Function PensionBenefit(OriginalServiceYear As Integer, OriginalSalary As Double, I As Integer, age As Integer)
PensionBenefit = 0.01 * ServiceYear(OriginalServiceYear, I, age) * AnnualPay(OriginalSalary, I, age)
End Function
Function rPVB_g(G As String, male_q As Variant, female_q As Variant, OriginalServiceYear As Integer, OriginalSalary As Double, age As Integer, base As Integer, year As Double, rate As Double)
Dim I As Integer
rPVB_g = 0
'If age >= 55 Then
For I = age To 120
If I >= 65 Then
rPVB_g = rPVB_g + PensionBenefit(OriginalServiceYear, OriginalSalary, age, base) * (1 + rate) ^ (-I + age) * cum_gp(G, male_q, female_q, age, year, I - age)
ElseIf I < 65 Then
rPVB_g = rPVB_g + PensionBenefit(OriginalServiceYear, OriginalSalary, age, base) * (1 + rate) ^ (-I + age) * cum_gp(G, male_q, female_q, age, year, I - age) '* EarlyRetireF(I)
End If
Next I
'End If
End Function
Function tvPVB_g(G As String, male_q As Variant, female_q As Variant, OriginalServiceYear As Integer, OriginalSalary As Double, age As Integer, base As Integer, year As Double, rate As Double)
Dim I As Integer
tvPVB_g = 0
' If age < 55 Then
For I = age To 120
If I < 65 Then
tvPVB_g = 0
ElseIf I >= 65 Then
tvPVB_g = tvPVB_g + PensionBenefit(OriginalServiceYear, OriginalSalary, age, base) * (1 + rate) ^ (-I + age) * cum_gp(G, male_q, female_q, age, year, I - age)
End If
Next I
' End If
End Function
Function DiscountFactor(rate As Double, I As Integer, age As Integer)
DiscountFactor = (1 + rate) ^ (-I + age)
End Function
Function aPVB_g(G As String, post_male_q As Variant, post_female_q As Variant, pre_male_q As Variant, pre_female_q As Variant, r_q As Variant, t_q As Variant, OriginalServiceYear As Integer, OriginalSalary As Double, age As Integer, year As Double, rate As Double)
Dim I As Integer
aPVB_g = 0
For I = age To 70
If I > 0 And I <= 54 Then
aPVB_g = aPVB_g + DiscountFactor(rate, I, age) * cum_totalP(G, pre_male_q, pre_female_q, r_q, t_q, age, year, I - age) * (terminate_q(t_q, I) * tvPVB_g(G, post_male_q, post_female_q, OriginalServiceYear, OriginalSalary, I, age, year + I - age, rate))
ElseIf I >= 55 Then
aPVB_g = aPVB_g + DiscountFactor(rate, I, age) * cum_totalP(G, pre_male_q, pre_female_q, r_q, t_q, age, year, I - age) * (retire_q(r_q, I) * rPVB_g(G, post_male_q, post_female_q, OriginalServiceYear, OriginalSalary, I, age, year + I - age, rate) * EarlyRetireF(I))
End If
Next I
End Function
Function BO(G As String, post_male_q As Variant, post_female_q As Variant, pre_male_q As Variant, pre_female_q As Variant, r_q As Variant, t_q As Variant, OriginalServiceYear As Integer, OriginalSalary As Double, age As Integer, year As Double, rate As Double)
Dim I As Integer
BO = 0
For I = age To 70
If I > 0 And I <= 54 Then
BO = BO + DiscountFactor(rate, I, age) * cum_totalP(G, pre_male_q, pre_female_q, r_q, t_q, age, year, I - age) * (terminate_q(t_q, I) * tvPVB_g(G, post_male_q, post_female_q, OriginalServiceYear, OriginalSalary, I, age, year + I - age, rate) * (ServiceYear(OriginalServiceYear, age, age) / ServiceYear(OriginalServiceYear, I, age)))
ElseIf I >= 55 Then
BO = BO + DiscountFactor(rate, I, age) * cum_totalP(G, pre_male_q, pre_female_q, r_q, t_q, age, year, I - age) * (retire_q(r_q, I) * rPVB_g(G, post_male_q, post_female_q, OriginalServiceYear, OriginalSalary, I, age, year + I - age, rate) * EarlyRetireF(I) * (ServiceYear(OriginalServiceYear, age, age) / ServiceYear(OriginalServiceYear, I, age)))
End If
Next I
End Function
Function ServiceCost(G As String, post_male_q As Variant, post_female_q As Variant, pre_male_q As Variant, pre_female_q As Variant, r_q As Variant, t_q As Variant, OriginalServiceYear As Integer, OriginalSalary As Double, age As Integer, year As Double, rate As Double)
Dim I As Integer
ServiceCost = 0
For I = age To 70
If I > 0 And I <= 54 Then
ServiceCost = ServiceCost + DiscountFactor(rate, I, age) * cum_totalP(G, pre_male_q, pre_female_q, r_q, t_q, age, year, I - age) * (terminate_q(t_q, I) * tvPVB_g(G, post_male_q, post_female_q, OriginalServiceYear, OriginalSalary, I, age, year + I - age, rate) * (1 / ServiceYear(OriginalServiceYear, I, age)))
ElseIf I >= 55 Then
ServiceCost = ServiceCost + DiscountFactor(rate, I, age) * cum_totalP(G, pre_male_q, pre_female_q, r_q, t_q, age, year, I - age) * (retire_q(r_q, I) * rPVB_g(G, post_male_q, post_female_q, OriginalServiceYear, OriginalSalary, I, age, year + I - age, rate) * EarlyRetireF(I) * (1 / ServiceYear(OriginalServiceYear, I, age)))
End If
Next I
End Function
````````````````````````````````````````````````````````````````````````````
Sub CopySheet()
'Updateby Extendoffice 20160704
'It can copy the number of active sheet you want and rename then orderly according to the number
Dim I As Long
Dim xNumber As Integer
Dim xName As String
Dim xActiveSheet As Worksheet
On Error Resume Next
Application.ScreenUpdating = False
Set xActiveSheet = ActiveSheet
xNumber = InputBox("Enter number of times to copy the current sheet")
For I = 1 To xNumber
xName = ActiveSheet.Name
xActiveSheet.Copy After:=ActiveWorkbook.Sheets(xName)
ActiveSheet.Name = "A" & I
Next
xActiveSheet.Activate
Application.ScreenUpdating = True
End Sub
Function TabName()
TabName = ActiveSheet.Name
End Function