vba - 在循环中使用 From Web Query

标签 vba excel excel-2013

我正在尝试从该网站提取数据:http://securities.stanford.edu/filings.html?page=1

每个“页面”是一个包含 21 个项目的表格。我想从 97 页中提取数据,但我无法将其自动化,以便宏循环遍历所有 97 页,并从单元格 A1 开始每 21 行放置一次结果。 (序列:a1、a22、a43 等...)

这是我得到的,但我不想编辑代码 97 时间来获取所有页面。知道如何使任务自动化吗?

Sub Macro1()
' Macro1 Macro
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://securities.stanford.edu/filings.html?page=1", Destination:=Range( _
        "A1"))
        .Name = "filings.html?page=1"**
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
end Sub

最佳答案

For x = 1 to 97
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://securities.stanford.edu/filings.html?page=" & x, Destination:=Range( _
        "A" & (1 + ((x - 1) * 21)))
        .Name = "filings.html?page=" & x
    End With
Next

x 包含页码,并且单元格很复杂,使其从 A1 而不是 A21 开始。

你可以做到0 to 96和单元格& (1 + (x + 21))以及名称和查询x + 1 .

关于vba - 在循环中使用 From Web Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147248/

相关文章:

vba - Microsoft Word 中的拼写错误

vba - 如何从 Excel 2003 VBA 写入 "Google Spreadsheet"

vba - 删除某种颜色的所有单元格

excel - SUMIF 函数多次引用

excel - 在 vba 用户窗体中使用不同的背景颜色

C#如何遍历excel列

java - 使用 Apache POI 创建受密码保护的 Excel 文件?

vba - 如何更新/更改所有条件规则范围的最后一行

excel - For Each 循环中的 Copy PasteSpecial 不规则退出

基于在另一个表中查找代码和值的 Excel 总和