|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
放眼TOT 发表于 2012-12-19 23:32
就是计算的股票买卖价格的两个利差。Spread1就是第43列的平均值。Spread2是求2*绝对值(P2-P1)/P2得出的 ...
你的原始数值有的为空,你录的宏计算Spread2总是等于0,程序修改如下- Sub test()
- Dim Arr, k&, ph$, fl$, sp1#, sp2#, brr, i&, x&, l&
- Dim crr(), n&
- ph = ThisWorkbook.Path & ""
- fl = Dir(ph & "*.txt")
- Do While fl <> ""
- Open ph & fl For Input As #1
- Arr = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
- Reset
- k = UBound(Arr) + 1: x = UBound(Arr): l = k
- sp1 = 0: sp2 = 0
- For i = 0 To x
- If Arr(i) <> "" Then
- brr = Split(Arr(i), ",")
- If brr(42) <> "" Then sp1 = sp1 + Val(brr(42)) Else k = k - 1
- If Val(brr(6)) <> 0 Then sp2 = sp2 + Abs(Val(brr(6)) - (Val(brr(24)) + Val(brr(25))) / 2) / Val(brr(6)) Else l = l - 1
- Else
- k = k - 1
- End If
- Next
- sp1 = sp1 / k: sp2 = sp2 / l
- n = n + 1
- ReDim Preserve crr(1 To 3, 1 To n)
- crr(1, n) = brr(0)
- crr(2, n) = sp1
- crr(3, n) = sp2
- fl = Dir
- Loop
- Sheet2.Range("a2:c" & Rows.Count).ClearContents
- Sheet2.Range("a2").Resize(n, 3) = Application.Transpose(crr)
- End Sub
复制代码 |
|