【问题标题】:VBA: Get/Download Excel file from server and copy contents into a new worksheetVBA:从服务器获取/下载 Excel 文件并将内容复制到新工作表中
【发布时间】:2012-12-20 13:46:07
【问题描述】:

由于某种原因,以下宏不起作用:

Sub ExtractDataTest()


    Dim FilePath$, Row&, Column&, Address$


 'change constants & FilePath below to suit
     '***************************************
    Const FileName$ = "Dxo.xlsx"
    Const SheetName$ = "Open"
    Const NumRows& = 50
    Const NumColumns& = 20
    FilePath = ("http://enhanced1.sharepoint.hx.com/teams/")
     '***************************************


    DoEvents
    Application.ScreenUpdating = False
        If Dir(FilePath & FileName) = Empty Then
        MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
        Exit Sub
    End If
    For Row = 1 To NumRows
        For Column = 1 To NumColumns
            Address = Cells(Row, Column).Address
            Cells(Row, Column) = GetData(FilePath, FileName, SheetName, Address)
            Columns.AutoFit
        Next Column
    Next Row
    ActiveWindow.DisplayZeros = False
End Sub




Private Function GetData(Path, File, Sheet, Address)
    Dim Data$
    Data = "'" & Path & "[" & File & "]" & Sheet & "'!" & _
    Range(Address).Range("A1").Address(, , xlR1C1)
    GetData = ExecuteExcel4Macro(Data)
End Function

我在线收到运行时错误“52”(“错误的文件名或编号”):

If Dir(FilePath & FileName) = Empty Then

有趣的是,它确实起作用了一次。当位置为“C:\”时,它可以工作并且我没有收到错误。奇怪的东西。

任何帮助将不胜感激。

【问题讨论】:

  • 在封闭的电子表格中多次查询数据效率非常低。为什么不打开,获取所有数据并粘贴到工作表上?

标签: excel vba copy worksheet


【解决方案1】:

从文件系统打开文件与通过 HTTP 下载文件完全不同。 最简单的不可知论方法是简单地使用允许 HTTP URI 的Workbooks.Open

Set wb = Workbooks.Open(FilePath & FileName)

(您需要删除Dir(),因为这仅适用于文件系统)

【讨论】:

  • 解决了我的问题。但现在我有一个新问题。宏将下载并打开文件,但他们会说文件 x 未找到。知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多