彭博数据历史上的VBAmacros观效率

我对VBA很新。 我写了这个代码,它的工作原理,但它需要很长的时间来运行,我感觉到,它越来越慢,越来越慢的运行。

有没有办法让这样的事情更有效率? 如果有人在这里有一些build议我会在这里真棒。

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 

删除Application.Wait (Now + TimeValue("0:00:03")) 。 或者把它放在下面,像这样:

 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次迭代。

=offset(Input!B1,row(),0)和B2是=BDH(ADDRESS(ROW(),1,4),""TURNOVER.....作为公式只是填补下来?

继我的评论关于硬编码。 下面是一些代码,以帮助不重复调用静态数据函数

 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 

对不起,你准备怎么做? 你想要股票的时间序列数据吗? 是的,请尝试下面的链接。

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

在这里输入图像说明