软件:excel vba
版本:2010
各位版大好,以下有一vba程式,目的是要抓取3万笔左右的交易价格进行统计分析(交易
价格可随机设为1000~10000之间的值)。
我的问题是,程式中有一段 For zzz = 0.95 To 1 Step k_gap,如果我将0.95改为0.9,
则原本百分比介于0.95~1的资料就没有跑出来,可是做循环时zzz却会取到1,觉得很困惑
,系望各位大大解惑,谢谢。
Sub slope_analysis()
Dim k_gap, percentile_lbound_value, percentile_ubound_value
Dim ary0(33000), ary1(33000), ary2(33000), ary3(3500), ary4(3500), _
ary5(30), ary6(30), ary7(500, 3)
Dim i&, j&, k&, m&, n&, p&
'60=3个月;120=6个月;180=9个月;250=1年;500=2年;750=3年;1000=4年;_
1250=5年
'1500=6年;1750=7年;2000=8年;2250=9年;2500=10年
k_gap = 0.05
j = 1
For i = 1 To 33000
If Cells(6 + i, 5) <> "" Then
ary0(j) = Cells(6 + i, 5)
j = j + 1
End If
Next
'zzz=百分比上限;xxx=历史天数;yyy=未来天数
For zzz = 0.95 To 1 Step k_gap
For xxx = 250 To 2500 Step 250
n = 0
Erase ary5, ary6
For yyy = 625 To 1250 Step 125
'若第i日前x天且后y天不超过资料范围,则分别写入第i日前x天与 _
后y天的报酬率
k = 0
m = 0
Erase ary1, ary2, ary3, ary4
For i = 1 To j - 1
If i - xxx > 0 And i + yyy <= j - 1 Then
ary1(i) = (ary0(i) - ary0(i - xxx)) / ary0(i - xxx)
ary2(i) = (ary0(i + yyy) - ary0(i)) / ary0(i)
k = k + 1
End If
Next
'求得历史报酬率样本中第i个百分比之下限值与第j个百分比之上限值
If zzz - k_gap = 0 Then
percentile_lbound_value = WorksheetFunction.Min(ary1)
Else
percentile_lbound_value = _
WorksheetFunction.Percentile(ary1, zzz - k_gap)
End If
If zzz = 1 Then
percentile_ubound_value = WorksheetFunction.Max(ary1)
Else
percentile_ubound_value = _
WorksheetFunction.Percentile(ary1, zzz)
End If
'将历史报酬率在百分比上下限内之值写入
For i = 1 + xxx To k + xxx
If ary1(i) > percentile_lbound_value And ary1(i) <= _
percentile_ubound_value Then
ary3(m) = ary1(i)
ary4(m) = ary2(i)
m = m + 1
End If
Next
ary5(n) = WorksheetFunction.Correl(ary3, ary4)
ary6(n) = n + 1
n = n + 1
Next yyy
ary7(p, 0) = zzz - k_gap
ary7(p, 1) = zzz
ary7(p, 2) = xxx
ary7(p, 3) = WorksheetFunction.Slope(ary5, ary6)
p = p + 1
Next xxx
Next zzz
For i = 0 To 500
Cells(6 + i, 9) = ary7(i, 0)
Cells(6 + i, 10) = ary7(i, 1)
Cells(6 + i, 11) = ary7(i, 2)
Cells(6 + i, 12) = ary7(i, 3)
Next
End Sub