选择Statement for Excel Datasource

时间:2015-05-27 14:12:01

标签: excel vba excel-vba adodb

我连接到Excel工作表,它充当数据库。我需要选择一些条件但我得到错误的记录:

  

没有给出一个或多个必需参数的值

使用以下代码:

Dim conn As Object
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set conn = CreateObject("ADODB.Connection")
XLName = "C:\Users\X\Desktop\rawdata.xlsx"
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & 
XLName   & "';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"
conn.Open connString
rs.Open ("SELECT * FROM [data$] where industry='Government'"), conn, 
adOpenDynamic, adLockReadOnly
Sheet1.Range("A2").CopyFromRecordset rs
rs.Close  
conn.Close

1 个答案:

答案 0 :(得分:1)

设置HDR = NO时,将忽略excel表中的列标题,并使用内部名称。请参阅旧答案:c#, oledb connection string issue