|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub sqlquery()
- Dim Cn As Object, ar
- ar = Sheet2.[a1].CurrentRegion
- Application.ScreenUpdating = False
- Set Cn = CreateObject("ADODB.Connection")
- Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
- 'Sq(1) = "[Export Worksheet (2)$]a"
- 'Sq(2) = "[Sheet1$]b"
- Sql = "SELECT [VIN],[Local/Import],[Model],[Model Year Description],[Subscription Package] FROM [Export Worksheet (2)$] WHERE [Local/Import]='" & Sheets("sheet1").[c2] & "' and [Model]='" & Sheets("sheet1").[b2] & "' and [Model Year Description]='" & Sheets("sheet1").[a2] & "'"
- 'Sq(0) = "SELECT a.[VIN],a.[Local/Import],a.[Model],a.[Model Year Description],a[Subscription Package] FROM " & Sq(1) & " LEFT JOIN " & Sq(2) & " ON a.[VIN]=b.[Vin]"
- Sheet3.Range("a2").CopyFromRecordset Cn.Execute(Sql)
- Cn.Close
- Set Cn = Nothing
- Application.ScreenUpdating = False
- MsgBox "OK!", 64
- End Sub
复制代码
where 后面引用的用法错了,然后查找的单元格也错了,A2跟C2弄反了,最后是Worksheets("sheet3")错了,这里哪里有一个叫sheet3的表呢?应该是Sheet3.Range("a2")或者Worksheets("sheet2")。 |
评分
-
1
查看全部评分
-
|