VBA-运行时间错误对象变量



我正在遇到以下代码的问题,特别是当击中运行时,我会收到错误消息"运行时间错误91:对象变量或块变量未设置"。

我附上了一张图片,说明了哪条线以黄色突出显示。谢谢

VBA代码,运行时间错误对象

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

无法告诉您什么或原因,但是我略微更改了它,以下操作有效:

    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

最新更新