Excel 连接到Oracle数据库以运行SQL语句的VBA代码

Excel 连接到Oracle数据库以运行SQL语句的VBA代码,excel,vba,Excel,Vba,您好,我需要连接我的Excel文件以运行执行select查询的宏 我的示例代码是 子按钮1\u单击() 范围(“A1”).Value=“Hiii!” 将cn作为对象 将遥感器作为对象 作为字符串的Dim strCon Dim strSQL,strInput为字符串 strCon=“Provider=MSDAORA.1;用户ID=bis5151XXXXXa1;数据源=canafpi5467qanfo.iaksedafdqwte.com;密码=ixxx8886s55it” Set cn=Create

您好,我需要连接我的Excel文件以运行执行select查询的宏

我的示例代码是
子按钮1\u单击()
范围(“A1”).Value=“Hiii!”
将cn作为对象
将遥感器作为对象
作为字符串的Dim strCon
Dim strSQL,strInput为字符串
strCon=“Provider=MSDAORA.1;用户ID=bis5151XXXXXa1;数据源=canafpi5467qanfo.iaksedafdqwte.com;密码=ixxx8886s55it”
Set cn=CreateObject(“ADODB.Connection”)
cn.开放式strCon
strInput=InputBox(“输入所需名称”)
表1.范围(“B1”)。值=划线
strSQL=“select*from”&strInput&“其中rownum
Sub Button1_Click()

Range("A1").Value = "Hiii!"

Dim cn As Object
Dim rs As Object

Dim strCon As String
Dim strSQL, strInput As String


strCon = "Provider=MSDAORA.1;User ID=bis5151XXXXXa1;Data Source=canafpi5467qanfo.iaksedafdqwte.com;Password=iXoXXX8886s55it"

Set cn = CreateObject("ADODB.Connection")
cn.Open strCon

strInput = InputBox("Input Desired Name")
Sheet1.Range("B1").Value = strInput
strSQL = "select * from " & strInput & " where rownum<200"
Sheet2.Range("D1").Value = strSQL
'Added the following four lines
Set rs = CreateObject("ADODB.RECORDSET")
rs.activeconnection = cn
rs.Open strSQL
Sheet2.Range("A10").CopyFromRecordset rs

rs.Close
cn.Close
Set cn = Nothing

End Sub