凭借彭博数据历史,VBA-宏观效率

时间:2017-08-16 09:25:14

标签: excel vba excel-vba bloomberg

我对VBA很新。我编写了这段代码并且它可以运行,但是需要很长时间才能完成,我感觉它运行的时间越来越慢。

有没有办法让这样的事情变得更有效率?如果有人在这里为我提供一些建议,那会很棒。

Sub Sheets()
Application.ScreenUpdating = False
ActiveWindow.WindowState = xlMinimized

Dim Data As String
Dim i As Long
Dim k As Long
Dim x As Long
Dim y As String


For i = 2 To 255

Sheetname = Worksheets("Input").Cells(i, 1).Value
Worksheets.Add.Name = Sheetname
ActiveSheet.Move After:=Worksheets(ActiveWorkbook.Sheets.Count)

x = 1

For k = 2 To 876
Data = Worksheets("Input").Cells(i, k).Value
y = Cells(1, x).Address(RowAbsolute:=False, ColumnAbsolute:=False)
BloomB = "=BDH(" & y & ",""TURNOVER"",""8/1/2011"",""4/30/2016"",""Dir=V"",""Dts=S"",""Sort=A"",""Quote=C"",""QtTyp=Y"",""Days=T"",""Per=cd"",""DtFmt=D"",""UseDPDF=Y"")"
Worksheets(Sheetname).Cells(1, x) = Data
Worksheets(Sheetname).Cells(2, x) = BloomB
x = x + 2
Next k

Application.Wait (Now + TimeValue("0:00:03"))
Next i

ActiveWindow.WindowState = xlMaximized
Application.ScreenUpdating = True
End Sub

4 个答案:

答案 0 :(得分:4)

删除Application.Wait (Now + TimeValue("0:00:03"))。或者将Next i放在它上面:

Next i
Application.Wait (Now + TimeValue("0:00:03"))

你将获得大约43分钟。

如果你仍然喜欢等待的想法,那就更复杂了,就像这样:

If i Mod 50 = 0 Then Application.Wait (Now + TimeValue("0:00:02"))
Next i

因此,它将每隔50次迭代等待。

答案 1 :(得分:0)

是不是所有的转化为A2都是=offset(Input!B1,row(),0)而B2是=BDH(ADDRESS(ROW(),1,4),""TURNOVER.....,因为公式刚被填满?

答案 2 :(得分:0)

继续我对硬编码的评论。这里有一些代码可以帮助不重复调用静态数据函数

Option Explicit

Sub Test()

    Dim rng As Excel.Range
    Set rng = Worksheets(1).Cells(1, 1)

    rng.Formula = "=SIN(PI()/4)"  '* a formula is in fact constant

    '* in some instances Evaluate will suffice
    Dim vHardcodedFormulaResult As Variant
    vHardcodedFormulaResult = Application.Evaluate("SIN(PI()/4)")

    rng.Offset(1, 0).Value2 = vHardcodedFormulaResult

    '* in other cases Application.Run will be needed and also multi-cell return results

    Dim vHardcodedFormulaResult2 As Variant
    vHardcodedFormulaResult2 = Application.Run("FakeBDH", "Citi", "FakeParam")

    Dim lIndex As Long
    lIndex = 0
    Dim vLoop As Variant
    For Each vLoop In vHardcodedFormulaResult2

        rng.Offset(2, lIndex) = vLoop
        lIndex = lIndex + 1
    Next vLoop


End Sub


Function FakeBDH(ByVal sKey As String, ByVal vParam1) As Variant
    FakeBDH = Array(2015, 45000)
End Function

答案 3 :(得分:0)

抱歉,你准备做什么呢?您想要股票的时间序列数据吗?是的,请尝试下面的链接。

http://investexcel.net/multiple-stock-quote-downloader-for-excel/

enter image description here