2015-05-06 100 views
0

我試圖從Excel中使用Excel VBA檢索數據,但我得到"run time error 424. object required"。 PFB源代碼。我對Excel VBA很新奇。我感謝您的幫助。無法生成數據使用EXCEL VBA(運行時錯誤424)

Sub Generate_Data() 
Dim str As String 
Dim UID As String 
Dim PWD As String 
Dim Server As String 
UID = "userid" 'Enter the User ID 
PWD = "pwd" 'Enter the password 
Server = "server" 'This comes from your TNSNames.ora file 
str = "PROVIDER=MSDAORA.Oracle;DATA SOURCE= & Server & ;Persist Security Info=True;USER ID= & UID & ;PASSWORD= & PWD &" 
Set cnn = CreateObject(「ADODB.Connection」) 
cnn.Open str 
Set RS = CreateObject(「ADODB.Recordset」) 
Row = 5 
col = 5 
numRs = RS.Fields.Count 
num = 0 
strSQL = "SELECT * FROM H1.TRADE_LOT_BASIS WHERE TO_CHAR(BOOK_VALUE_DATE,'YYYY')>=TO_CHAR(SYSDATE,'YYYY')" 
    RS.Open strSQL, cnn 
    Do Until RS.EOF 
    Do While num < numRs 
    Cells(Row, col + num).Value = RS(num) 
    num = num + 1 
    Loop 
    num = 0 
    Row = Row + 1 
    RS.moveNext 
    Loop 
    RS.Close 
    cnn.Close 
    End Sub" 
+0

在這行你有沒有錯誤? – R3uK

+0

我在這行上有錯誤。 Set cnn = CreateObject(「ADODB.Connection」) –

回答

0

試試這個:

Dim oConn As New ADODB.Connection 
Dim RS As New ADODB.Recordset 

Set oConn = New ADODB.Connection 
相關問題