首页 > 解决方案 > 我在基于表格的 Excel Web 查询的 vba 代码中遇到了语法错误

问题描述

我正在尝试更新我记录的宏,以便电子表格中的行中的内容将更新 Web 查询目标,但遇到了一些错误。我已经对其中的几个进行了排序,但当前的问题是运行时错误。以下是我的查询代码:

Sub GetAllPkgInfo()

  Sheets("AllPkgs").Select
  Range("A2").Select

  Do Until ActiveCell.Value = ""
    Call PkgInfo1
  Loop


End Sub



Sub PkgInfo1()
'
' PkgInfo1 Macro
' Import RHEL package information from RPMFind
'
' Keyboard Shortcut: Ctrl+Shift+D
'
    Selection.Copy
    Sheets("WebQuery").Select
    ActiveSheet.Cells.Clear
    Range("$G$1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add( _
    Connection:="URL;https://rpmfind.net/linux/rpm2html/search.php?query=" & _
       Range("$G$1").Value, Destination:=Range("$A$1"))
        .CommandType = 0
        .Name = "search.php?query=abrt_1"
        .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 = "2"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveCell.Offset(1, -5).Range("A1").Select
    Selection.Copy
    Sheets("AllPkgs").Select
    ActiveCell.Offset(0, 1).Range("CombinedPackages[[#Headers],[Column1]]").Select
    ActiveSheet.Paste
    Sheets("WebQuery").Select
    ActiveCell.Cells.Select
    Application.CutCopyMode = False
    Selection.QueryTable.Delete
    Selection.ClearContents
    Sheets("AllPkgs").Select
    ActiveCell.Offset(1, -1).Range("CombinedPackages[[#Headers],[Column1]]").Select
End Sub

我收到的错误消息指出“运行时错误'1004':工作表类的粘贴方法失败”并突出显示第一个代码实例

    ActiveSheet.Paste

有人可以帮我理顺吗?

标签: vbaexcelweb

解决方案


With ActiveSheet.QueryTables.Add( _
    Connection:= "URL;https://rpmfind.net/linux/rpm2html/search.php?query=" & _
       Range("G1").Value, Destination:=Range("$A$1"))

推荐阅读