简体   繁体   English

使用 Excel 作为数据库从多个工作表中提取数据

[英]Pull data from multiple worksheets using Excel as database

It would be really helpful if you advise me on how to query and pull data from multiple excel worksheets using ADODB record set.如果您建议我如何使用 ADODB 记录集从多个 Excel 工作表中查询和提取数据,那将非常有帮助。 Below is my VBA script, it's throwing me "Type Mismatch Error".下面是我的 VBA 脚本,它向我抛出“类型不匹配错误”。

VBA Code: VBA代码:

Dim sSQLQry As String
Dim conn As New ADODB.connection
Dim mrs As New ADODB.Recordset
Dim DBpath As String
Dim sconnect As String

Private Sub Generate_Testcase_Click()
DBpath = ThisWorkbook.FullName
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBpath & ";HDR=Yes';"
conn.Open sconnect

sSQLQry = "SELECT TOP 1 [Sheet1$].EMPLOYEEID FROM [Sheet1$] INNER JOIN [Sheet2$] ON [Sheet1$].EMPLOYEEID =[Sheet2$].PPTID AND [Sheet2$].UNION_CD =[Sheet1$].UNID"

mrs.Open sSQLQry, conn, adOpenStatic, adLockReadOnly
MsgBox mrs.Fields(0) & " , " & mrs.Fields(1)
mrs.Close
conn.Close
End Sub

Also I tried with below query as well, but still same errror.我也尝试了以下查询,但仍然出现同样的错误。

sSQLQry = "SELECT TOP 1 [Sheet1$].EMPLOYEEID FROM [Sheet1$], [Sheet2$] WHERE [Sheet1$].EMPLOYEEID =[Sheet2$].PPTID AND [Sheet2$].UNION_CD =[Sheet1$].UNID"

Please help me to fix the issue.请帮我解决问题。

Finally, I found the issue, both the columns [Sheet1$].EMPLOYEEID and [Sheet2$].PPTID are in a different format.最后,我发现了问题,两列 [Sheet1$].EMPLOYEEID 和 [Sheet2$].PPTID 的格式不同。 The issue was fixed once I changed both columns to the same format.一旦我将两列都更改为相同的格式,问题就得到了解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM