2017-02-19 100 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

相关问题