我遇到了以下代码的问题,特别是在点击运行时我收到错误消息“运行时错误91:对象变量或未设置块变量”。
我附上了黄色突出显示的图片。感谢
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 :(得分: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