HTML到Excel+;换页循环
错误显示未设置HTML到Excel+;换页循环,html,excel,vba,loops,getelementbyid,Html,Excel,Vba,Loops,Getelementbyid,错误显示未设置对象变量或With block变量。但当我试图将doc定义为文档或word文档时,它不允许这样做。提前感谢 Sub HTMLtoExcel() Dim doc As Object Dim i As String i = 1 Do While i <= 1783 Set doc.getElementById("Pages").Value = CStr(i) With ActiveSheet.QueryTables.Add(Connection:= _ "UR
对象变量或With block变量
。但当我试图将doc定义为文档或word文档时,它不允许这样做。提前感谢
Sub HTMLtoExcel()
Dim doc As Object
Dim i As String
i = 1
Do While i <= 1783
Set doc.getElementById("Pages").Value = CStr(i)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http:/xxx.yyy" _
, Destination:=Range("$A$1"))
.Name = _
"its_details_value_node.html?nsc=true&listId=www_s201_b9233&tsId=BBK01.ED0439"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
i = i + 1
Loop
End Sub
Sub-HTMLtoExcel()
Dim doc作为对象
我像绳子一样暗
i=1
当我doc
是Nothing
时做什么。这一行正在中断,因为您实际上在执行Set Nothing.getElementById(“Pages”).Value=CStr(i)
但当我试图将doc定义为文档或word文档时
您需要澄清发生了什么,因为这不是可以用于Word文档
对象的语法,而是一个网页.Document
。由于您使用的是QueryTables
,因此我假设后者:
为了解决这个问题,您需要首先创建一个web浏览器实例,并通过该实例设置doc
,如:
Dim ie as Object
Set ie = CreateObject("Internet.Explorer")
ie.Navigate "http://google.com" 'Modify as needed
Do While ie.ReadyState <> 4 And Not ie.Busy
DoEvents
'It is better to use the WinAPI sleep function
' there are several examples of implementing this here on SO
Loop
Set doc = ie.Document
对于初学者来说,.Value
不是一个对象,因此不能设置它。另外,getElementByID
返回一个对象。也许你想要:
doc.getElementById("Pages").Value = Cstr(i)
现在您有一个执行1783次的循环,但它不会像您预期的那样工作,因为您没有更改QueryString
中的任何参数,因此如果它确实执行1783次,它每次都会拉入相同的数据。如果你在这方面有困难,你应该把它作为一个单独的/新的问题来问
您还需要将i设置为整数
或长
,而不是在此上下文中设置为字符串,否则此行可能会出现不匹配错误:i=i+1
不知道我是否可以回答您的问题,但我有一些想法。“我”不应该被称为“长”或“双”吗。另外,您不需要将“暗化”“CStr”作为一个数组吗?
doc.getElementById("Pages").Value = Cstr(i)