我正在尝试编写一个宏来将服务器上托管的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
我唯一的问题是它会弹出以下对话框。
我每次都需要选择'MTD SAR $'选项。我可以在vba代码中选择此选项以避免该对话框吗?任何帮助将不胜感激。
答案 0 :(得分:0)
我不记得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