【问题标题】:Run Time Error 5: Invalid Procedure Call or Argument运行时错误 5:无效的过程调用或参数
【发布时间】:2018-07-25 04:28:48
【问题描述】:

我收到了

运行时错误 5:无效的过程调用或参数

在这一行:

With ActiveSheet.QueryTables.Add(Connection:=User_File_Path, Destination:=Range("$A$1"))

我在该行之前有一个消息框,告诉我正在填充的 User_File_Path,当我将鼠标悬停在 Connection:=User_File_Path 上时,它也会显示文件路径......不知道如何解决这个问题。任何帮助表示赞赏!

【问题讨论】:

    标签: excel vba variables runtime-error


    【解决方案1】:

    试试这样:

    With ActiveSheet
          Dim someTable As QueryTable
          Set someTable = .QueryTables.Add(Connection:=User_File_Path, _
                                              Destination:=Range("$A$1"))
    End With
    

    这里是 QueryTables.Add() 方法的 MSDN 文章:

    https://msdn.microsoft.com/en-us/vba/excel-vba/articles/querytables-add-method-excel

    如果您的连接是本地 csv 文件,则此方法有效:

    Public Sub TestMe()   
    
        Dim someTable As QueryTable
        Set someTable = ActiveSheet.QueryTables.Add(Connection:= _
                        "TEXT;C:\MyText.csv", _
                        Destination:=Range("$A$1"))
    
        someTable.Refresh
    End Sub
    

    【讨论】:

    • 嗨,Vityata - 不幸的是,没有运气。当我这样布置它时,它会突出显示 QueryTable 行红色。
    • 嗨,Vityata-我是通过录制手动版本来解决的。我错过了这个:“文本;” & User_File_Path & User_File_Name ...否则,我的格式有效。感谢您的帮助!
    【解决方案2】:

    解决方案:

    "Text;" & User_File_Path & User_File_Name

    With ActiveSheet.QueryTables.Add(Connection:="Text;" & User_File_Path & User_File_Name, Destination:=Range("$A$1"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多