0

您好,我在行中收到错误 [Runtime Error '448': Named argument not found]

Set qt = ThisWorkbook.Sheets(server_hostname).ListObjects.Add(Connection:=oRS, _
Destination:=ThisWorkbook.Sheets(server_hostname).Range("A1")).QueryTable

完整的源代码如下,

Sub getavgcpu(server_hostname)

Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String


Dim qt As QueryTable
ThisWorkbook.Sheets(server_hostname).Activate

ConnString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost; _
Database=test; User=root;Password=123456;Option=3;"
Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.Open

SQL = "SELECT cpu_max_unix_0.LOGDATE as 'Date of Month', cpu_max_unix_0.CPU as 'CPU   Utilization %' FROM test.cpu_max_unix cpu_max_unix_0 where (cpu_max_unix_0.LOGDATE between '" & fromDateStr & "' and '" & toDateStr & "') And  (cpu_max_unix_0.SERVER_NAME='" & server_hostname & "') Order By cpu_max_unix_0.LOGDATE"

Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCn
oRS.Open

'Set qt = ThisWorkbook.Sheets(server_hostname).QueryTables.Add(Connection:=oRS, _
'Destination:=ThisWorkbook.Sheets(server_hostname).Range("A1"))

Set qt = ThisWorkbook.Sheets(server_hostname).ListObjects.Add(Connection:=oRS, _
Destination:=ThisWorkbook.Sheets(server_hostname).Range("A1")).QueryTable



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

请帮忙!

4

2 回答 2

1

I tried your code with the first argument(SourceType) as following:

Set qt = ThisWorkbook.Sheets(server_hostname).ListObjects.Add( _
    SourceType:=XlListObjectSourceType.xlSrcQuery, _
        Source:=oRS, _
            Destination:=ThisWorkbook.Sheets(server_hostname).Range("A1")).QueryTable

and it works well.

于 2012-05-22T00:15:25.793 回答
0

未经测试

尝试这个

Dim ws As Worksheet

'
'~~> Rest of the code
'

Set ws = ThisWorkbook.Sheets(server_hostname)

Set objListObject = ws.ListObjects.Add(SourceType:=xlSrcExternal, Source:=oRS, _
LinkSource:=True, TableStyleName:=xlGuess, Destination:=ws.Range("A1"))

'
'~~> Rest of the code
'

另请参阅 MS 的此链接

主题:ListObjects.Add 方法 (Excel)

链接:http: //msdn.microsoft.com/en-us/library/ff823155.aspx

于 2012-05-21T23:40:16.947 回答