2017-02-19 101 views
1

我遇到了以下代碼的問題,特別是在運行時遇到錯誤消息「運行時錯誤91:對象變量或塊阻止變量未設置」。VBA - 運行時錯誤對象變量

我附上了以黃色突出顯示哪一行的圖片。由於

VBA Code, run time error object

Sub Data_Get() 

Dim ActiveSheet As Worksheet 
Dim EndDate As Date 
Dim StartDate As Date 
Dim Symbol As String 
Dim qurl As String 
Dim nQuery As Name 
Dim LastRow As Integer 

Application.ScreenUpdating = False 
Application.DisplayAlerts = False 
Application.Calculation = xlCalculationManual 

Columns("B:G").ClearContents 

StartDate = Range("K2").Value 
EndDate = Range("K3").Value 
Symbol = Range("K1").Value 


qurl = "http://finance.google.com/finance/historical?q=" & Symbol 
qurl = qurl & "&startdate=" & MonthName(Month(StartDate), True) & _ 
     "+" & Day(StartDate) & "+" & Year(StartDate) & _ 
     "&enddate=" & MonthName(Month(EndDate), True) & _ 
     "+" & Day(EndDate) & "+" & Year(EndDate) & "&output=csv" 

QueryQuote: 
With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=Range("b1")) 
    .BackgroundQuery = True 
    .TablesOnlyFromHTML = False 
    .Refresh BackgroundQuery:=False 
    .SaveData = True 
End With 

Range("B1").CurrentRegion.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _ 
                 TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ 
                 Semicolon:=False, Comma:=True, Space:=False, other:=False 

Columns("B:G").ColumnWidth = 12 

LastRow = ActiveSheet.UsedRange.Row - 2 + ActiveSheet.UsedRange.Rows.Count 

ActiveSheet.Sort.SortFields.Add Key:=Range("B2:B" & LastRow), _ 
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 

With ActiveSheet.Sort 
    .SetRange Range("B1:G" & LastRow) 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
    .SortFields.Clear 
End With 

End Sub 
+0

請使用標籤問題輸入驗證碼而不是隻是插入一個圖像的鏈接。如果你_do_鏈接一個圖像,實際上輸入一個圖像描述,而不是「在這裏輸入圖像描述」可能也會有所幫助... –

+0

我新來這個,我試着粘貼代碼,並不斷收到錯誤消息。 – Texcel

+0

好的,當您將代碼放在標記中時會發生什麼?只需粘貼您的代碼,將其標記並點擊問題編輯窗口頂部的代碼按鈕即可。 –

回答

0

不能告訴你爲什麼,但我改變了它略有上升,並以下工作:

Sub Data_Get() 

Dim ws As Worksheet 
Dim EndDate As Date 
Dim StartDate As Date 
Dim Symbol As String 
Dim qurl As String 
Dim nQuery As Name 
Dim LastRow As Integer 

Application.ScreenUpdating = False 
Application.DisplayAlerts = False 
Application.Calculation = xlCalculationManual 

Columns("B:G").ClearContents 

Set ws = ActiveSheet 

StartDate = ws.Range("K2").Value 
EndDate = ws.Range("K3").Value 
Symbol = ws.Range("K1").Value 
Range("b1").CurrentRegion.ClearContents 


qurl = "http://finance.google.com/finance/historical?q=" & Symbol 
qurl = qurl & "&startdate=" & MonthName(Month(StartDate), True) & _ 
     "+" & Day(StartDate) & "+" & Year(StartDate) & _ 
     "&enddate=" & MonthName(Month(EndDate), True) & _ 
     "+" & Day(EndDate) & "+" & Year(EndDate) & "&output=csv" 

QueryQuote: 
With ws.QueryTables.Add(Connection:="URL;" & qurl, Destination:=Range("b1")) 
    .BackgroundQuery = True 
    .TablesOnlyFromHTML = False 
    .Refresh BackgroundQuery:=False 
    .SaveData = True 
End With 

ws.Range("b1").CurrentRegion.TextToColumns Destination:=ws.Range("b1"), DataType:=xlDelimited, _ 
                 TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ 
                 Semicolon:=False, Comma:=True, Space:=False, other:=False 

ws.Columns("B:G").ColumnWidth = 12 

LastRow = ws.UsedRange.Row - 2 + ws.UsedRange.Rows.Count 

ws.Sort.SortFields.Add Key:=Range("B2:B" & LastRow), _ 
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 

With ws.Sort 
    .SetRange Range("B1:G" & LastRow) 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
    .SortFields.Clear 
End With 

End Sub 
+0

感謝您的幫助,所有 – Texcel

相關問題