Excel宏将Web数据转换为单个单元格(Excel macro to get web data into single cell)

编程入门 行业动态 更新时间:2024-10-20 03:46:04
Excel宏将Web数据转换为单个单元格(Excel macro to get web data into single cell)

我有一个电子表格,其中包含A列中的URL列表。我想读取每个URL并将输出存储在单个单元格中,或者至少存储在与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

I have a spreadsheet with a list of URLs in column A. I want to read each URL and store the output in a single cell, or at least into the same row as the URL. I am currently using the following standard VBA code to do each read (simplified here to use a static URL).

The problem is Excel is automatically separating the data into multiple rows and multiple columns, depending on the format of the web page (which is obviously what would usually be wanted).

Is there any simple way of modifying that code to force the output into a single cell or row please?

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

Don't worry, I managed to figure it out using code from here: Fetch specific table only from website into Excel

My version below assumes column A contains a list of URLs, and columns B-I contain other data. I want to read the web page for each URL from column A and pull out 2 fields from a table and put them in columns J and K (columns 10 and 11). The fields are named ID="Name1" and ID="Name2" in the web pages.

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

最满意答案

显然它与文本到列有关。 如果之前已使用过,并且将空白指定为分隔符,则它将应用于将来的数据导入。 奇怪的。 无论如何只需进入Text to Columns并删除空白分隔符并接受,然后重做数据导入。

Apparently it relates to Text to Columns. If that has been used previously and a blank was specified as the separator then it gets applied to future data imports. Weird. Anyway just go into Text to Columns and remove the blank separator and accept, then redo the data import.

更多推荐

本文发布于:2023-08-03 14:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   单元格   数据   Web   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!