Excel - 自动从网站获取数据到Excel

时间:2016-07-06 12:32:29

标签: excel

我正在尝试编写可自动从网站获取数据的代码, 我想用一个单元格中的任何文本替换URL地址的最后一部分(文本:“22A”),例如“G1”

录制宏中的代码:

    Sub USA()
'
' USA N-numbers Macro
'

'
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www6.landings.com/cgi-bin/nph-search_nnr?pass=193800885&&nnumber=22A" _
, Destination:=Range("$A$1"))
.Name = "nph-search_nnr?pass=193800885&&nnumber=22A"
.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 = "18"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Columns("A:A").ColumnWidth = 28.29
Columns("B:B").ColumnWidth = 4.57
End Sub

当我用Range(“G1”)替换22A时。我得到的值编译错误:“预期:列表分隔符或)” 有人知道我的编码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

替换:

"URL;http://www6.landings.com/cgi-bin/nph-search_nnr?pass=193800885&&nnumber=22A"

使用:

"URL;www6.landings.com/cgi-bin/nph-search_nnr? pass=193800885&&nnumber=" _ & Range("G1").Value