【问题标题】:Importing an excel file on a server into another excel file将服务器上的 excel 文件导入另一个 excel 文件
【发布时间】:2013-02-17 12:42:58
【问题描述】:

我正在尝试编写一个宏来将托管在服务器上的 excel 文件导入工作簿。这是我到目前为止的代码。

Sub ranker()
'
' ranker macro
'

'
    Range("Ranker!A10:Ranker!Z100").ClearContents
    URL = Range("url!F2" & i).Text
    With ActiveSheet.QueryTables.Add(Connection:= _
        "FINDER;" & URL _
        , Destination:=Range("Ranker!$A$1:$Z$100"))
        .Name = "ranker"
        .CommandType = xlCmdTable
        .CommandText = Array("'MTD SAR$'")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

我唯一的问题是它会弹出以下对话框。

dialog box

我每次都需要选择“MTD SAR$”选项。我可以在 vba 代码中选择此选项以避免该对话框吗?任何帮助将不胜感激。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我不记得querytable 方法能够指定您想要的工作表。我已经使用 OLEDB 解决了这个问题。这是一个例子:

    Sub Excel_QueryTable()
    
        Dim oCn As ADODB.Connection
        Dim oRS As ADODB.Recordset
        Dim ConnString As String
        Dim SQL As String
    
        Dim qt As QueryTable
    
        ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\SubFile.xls;Extended Properties=Excel 8.0;Persist Security Info=False"
        Set oCn = New ADODB.Connection
        oCn.ConnectionString = ConnString
        oCn.Open
    
        SQL = "Select * from ['MTD SAR$']"
    
        Set oRS = New ADODB.Recordset
        oRS.Source = SQL
        oRS.ActiveConnection = oCn
        oRS.Open
    
        Set qt = Worksheets(1).QueryTables.Add(Connection:=oRS, _
    Destination:=Range("Ranker!$A$1"))
    
        qt.Refresh
    
        If oRS.State <> adStateClosed Then
            oRS.Close
        End If
    
    
        If Not oRS Is Nothing Then Set oRS = Nothing
        If Not oCn Is Nothing Then Set oCn = Nothing
    
    End Sub
    

    这是我使用的,当然是经过调整的:Query table with Excel as Data Source

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多