2012-06-07 49 views
0

我写了一个vba来传递范围的偏移量作为另一个函数的查询表的输入,但是我遇到错误。Excel vba如何将范围传递给函数从mysql中检索数据

Sub test2() 

Dim LastCol As Long 
With ThisWorkbook.Sheets("Summary (AIX)") 
LastCol = .Range("A2").SpecialCells(xlCellTypeLastCell).Column 
End With 

Dim r As Long 
Dim ser_name As String 
Dim y As String 

For r = (LastCol - 1) To 2 Step -2 
With ThisWorkbook.Sheets("Summary (AIX)") 
y = Cells(2 + 2, r + 1).Address(RowAbsolute:=False, ColumnAbsolute:=False) 

MsgBox CStr(Cells(2, r).Value) 
ser_name = CStr(Cells(2, r).Value) 
get_unix_avg_cpu ser_name, y 
End With 
Next r 
End Sub 

=========================================== ==============================上

Sub get_unix_avg_cpu(server_hostname, x) 


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.CPU FROM test.cpu_max_unix cpu_max_unix_0 where 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("Summary (AIX)").QueryTables.Add(Connection:=oRS, _ 
Destination:=ThisWorkbook.Sheets("Summary (AIX)").Range(x)) 


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 

停止 “子get_unix_avg_cpu(server_hostname,X)” 编译错误:用户定义类型未定义

请帮忙!!

+0

您是否添加了对Microsoft活动数据对象库的引用? – SWa

+0

不..我不知道如何添加 –

回答

0

ADODB是一个外部库,这意味着它不是内置在Excel中,因此您需要将它添加到您的项目中。

要做到这一点,在IDE中点击工具>参考

然后向下滚动,直到找到“Microsoft ActiveX数据对象”,就会有一些这类所以挑一个数字最高,剔然后按OK。

然后,您应该能够运行您的代码。

这种方法被称为早期绑定,并为您的工作簿为其他人工作,他们也需要在他们的计算机上安装此参考。一般来说,开发时最好使用运行代码的最低版本的库。你也可以使用后期绑定。

+0

嗨凯尔谢谢你!! –

相关问题