Excelmacros将Web数据转换为单个单元格

我有一个电子表格,列A中的URL列表。我想要读取每个URL并将输出存储在单个单元格中,或者至less将其存储在与URL相同的行中。 我目前使用下面的标准VBA代码来完成每个读取(这里简化为使用静态URL)。

问题是Excel根据网页的格式自动将数据分成多行和多列(这显然是通常需要的)。

有没有简单的方法来修改该代码强制输出到单个单元格或行请?

Sub FetchData() With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://www.someurl", Destination:=Range("$B$1")) .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 End Sub 

不用担心,我设法从这里使用代码: 只从网站获取特定的表格到Excel中

我的下面的版本假设列A包含一个URL列表,列BI包含其他数据。 我想读取列A中每个URL的网页,并从表中取出2个字段,并将它们放入J和K列(第10和11列)。 这些字段在网页中被命名为ID =“Name1”和ID =“Name2”。

 Sub Getfromweb() Dim RowNumb As Long Dim LastRow As Long Dim htm As Object Set htm = CreateObject("htmlFile") LastRow = Cells(Rows.Count, 1).End(xlUp).Row For RowNumb = 1 To LastRow With CreateObject("msxml2.xmlhttp") .Open "GET", Sheets(1).Cells(RowNumb, 1), False .send htm.body.innerhtml = .responsetext End With With htm.getelementbyid("Name1") Sheets(1).Cells(RowNumb, 10).Value = .innertext End With With htm.getelementbyid("Name2") Sheets(1).Cells(RowNumb, 11).Value = .innertext End With Next RowNumb End Sub 

显然它涉及到文本到列。 如果之前已经使用过,并且指定了空白作为分隔符,那么它将应用于将来的数据导入。 奇怪的。 无论如何,只需进入“文本到列”并删除空白分隔符并接受,然后重新导入数据。