2016-05-20 149 views
0

我有一個宏,它根據公司名稱從雅虎財務網站獲取信息並將其放入Excel中。當我使用F8運行它時,宏和Excel運行良好。但是,當我嘗試使用F5來運行它(沒有中斷)時,它不會超出第5次迭代(有5.5k次迭代要完成)。MS Excel在運行VBA時崩潰

我正在運行的筆記本電腦是一款帶有i-7 2670QM芯片@ 2.2GHz,8GB RAM和64位操作系統(Win 7)的戴爾XPS。在MS Excel是2013年

的代碼如下:

Sub Yahoo_Company_List() 

Application.ScreenUpdating = False 

On Error GoTo ErrorHandler 

a = 3 

'While Worksheets("Storage Sheet").Cells(a, 1) <> vbNullString 
While a < 10 

    Worksheets("Downloads").Activate 
    Columns.Select 
    Selection.ClearContents 

    Symbol = Worksheets("Storage Sheet").Cells(a, 1) 

    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;https://uk.finance.yahoo.com/q/is?s=" & Symbol & "&annual", Destination:=Range(_ 
     "$A$1")) 
     .Name = "is?s=" & Symbol & "&annual" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .WebSelectionType = xlSpecifiedTables 
     .WebFormatting = xlWebFormattingNone 
     .WebTables = "9" 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 
    End With 

    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;http://finance.yahoo.com/q/bs?s=" & Symbol & "+Balance+Sheet&annual", Destination _ 
     :=Range("$A$41")) 
     .Name = "bs?s=" & Symbol & "+Balance+Sheet&annual" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .WebSelectionType = xlSpecifiedTables 
     .WebFormatting = xlWebFormattingNone 
     .WebTables = "9" 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 
    End With 

    Range("A91").Select 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;http://finance.yahoo.com/q/cf?s=" & Symbol & "+Cash+Flow&annual", Destination:= _ 
     Range("$A$91")) 
     .Name = "cf?s=" & Symbol & "+Cash+Flow&annual" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .WebSelectionType = xlSpecifiedTables 
     .WebFormatting = xlWebFormattingNone 
     .WebTables = "9" 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 
    End With 

    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;https://uk.finance.yahoo.com/q?s=" & Symbol & "&ql=1", Destination:=Range("$A$122")) 
     .Name = "q?s=" & Symbol & "&ql=1_1" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .WebSelectionType = xlSpecifiedTables 
     .WebFormatting = xlWebFormattingNone 
     .WebTables = """table1"",""table2""" 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 
    End With 

    Call Reformatting_m.reformatting 

    Worksheets("Calculations").Activate 

    Range("B:F").Select 
    Selection.ClearContents 

    i = 1 

    While i < 109 
     m = 1 
     If Cells(i, 1) <> vbNullString Then 
      While m <= 3 
       DataValue = WorksheetFunction.VLookup(Cells(i, 1), Worksheets("Downloads").Range("A1:F200"), 1 + m, False) 
       If Not IsError(DataValue) Then 
        Cells(i, 1 + m) = DataValue 
       End If 

       If Cells(i, 1) = "Period Ending" Then 
        Cells(i, 1 + m).NumberFormat = "m/d/yyyy" 
       Else 
        Cells(i, 1 + m).NumberFormat = 0 
       End If 
       m = m + 1 
      Wend 
     End If 
     i = i + 1 
    Wend 

    Call FScore_m.FScoreCalc 

' Application.Calculate 

    Worksheets("Storage Sheet").Activate 

    n = 5 
    k = 8 
    p = 2 


    While n < 67 
     If ((p = 9 Or p = 10 Or p = 11 Or p = 12 Or p = 13 Or p = 27) And k = 10) Or k = 11 Or _ 
      ((p = 21 Or p = 22 Or p = 23 Or p = 24 Or p = 25 Or p = 26) And k = 9) Then 
      k = 8 
      p = p + 1 
     ElseIf k < 11 Then 
      Cells(a, n) = Worksheets("Calculations").Cells(p, k) 
      k = k + 1 
      n = n + 1 
     End If 
    Wend 

    a = a + 1 

Wend 

Application.ScreenUpdating = True 

ErrorHandler: 
Application.ScreenUpdating = True 
Exit Sub 

End Sub 

如何得到它的工作的宏觀工作有什麼建議?

+0

最好我可以說它在代碼中設置休息,以查看哪個點使excel崩潰,它沒有給你一個沒有足夠內存的錯誤,所以它只是一個令你不安的excel代碼,找到這行後的代碼給你的問題,要麼你將能夠解決這個問題,或者我們可以進一步提供幫助 –

+0

對於Symbol = Worksheets(「存儲表」)的特定值,查詢可能會失敗,單元格(a,1)',也許是第六個價值。該代碼執行錯誤/符號的空值失敗 –

回答

1

我不能給你一個完整的答案,因爲我們無法訪問Call程序中的代碼(例如Reformatting_m.reformatting),他們可能會導致這個問題,但我有一些類似的東西在一些廣泛的Word自動化它幾乎就像是內存不足,會「隨機」崩潰。

我強烈建議的最佳建議是創建變量並在其中工作。例如: -

那樣工作導致工作簿意義的資源將是免費的,可能不會發生崩潰更少的連接。