回答

收藏

从宏查询(加入)外部wokbooks

技术问答 技术问答 490 人阅读 | 0 人回复 | 2023-09-11

我有3个Excel工作簿:
. ]+ z: i# G2 M6 I! D) F宏(完成后将作为外接程序)
( q6 c2 N# s! L2 {: \0 B8 }7 A前期数据清单
3 f. P! K! H5 T: X, ^. N. X当前期间数据列表; Z7 W% z% v4 L7 \3 S* ?( J/ k! F
这两个列表均少于1000行。
" }* _4 {% ]! D' F$ L" y
8 {- V- g3 j/ g6 \
我正在尝试join在这些列表之间创建一个SQL ,但无法正常工作。由于工作表名称中包含点(例如“ TB 03.18”),因此我使用了技巧-
% [- N- I8 b/ M2 R" n7 V5 [# R) F2 Z在SO的某处找到-在相应的工作簿中定义命名范围。
$ l6 _/ w8 ^/ s3 v下面的代码包含有效的2条SQL信息和给出错误的1条信息。我搞不清楚了。
! v1 G. \0 n' n! W1 Y'set connection to 'current tb'- ^3 s$ D* M5 }% E  F* C
sConn = &quotrovider=Microsoft.ACE.OLEDB.12.0;Data Source=" & wbCurr.FullName & _
0 E0 q$ A, E" n$ W8 h$ ^        ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
1 q; f  F! u9 z& HSet con = New ADODB.Connection8 h8 h2 R$ A/ x
con.Open sConn
. S4 `; y/ w' [1 P6 u4 T! mwbCurr.Names.Add Name:="cur", RefersTo:=shCurr.Range("A4").CurrentRegion    'to be made dynamic !!!& p( i4 C5 v4 p, B( i
wbPrev.Names.Add Name:="pre", RefersTo:=shPrev.Range("A9").CurrentRegion    'to be made dynamic !!!
$ z6 y$ p$ M1 |" k  I! j, [lastRow = shPrev.Range("a15000").End(xlUp).Row7 g. ~  \) c  \0 n3 p' N& b
shCurr.Range("F5:I15000").Clear
0 Q4 I4 W3 s$ u7 Y) k$ k+ A6 L7 XDim xl12 As String: xl12 = "[Excel 12.0rovider=Microsoft.ACE.OLEDB.12.0;Extended Properties='HDR=YES';] "+ B0 U# d; G* h( p
1 a( S* g5 v- W% x
'this works ------------------* T0 W( H* [; O% B6 `
'sSql = "select reference from  cur " & vbCrLf & _5 b3 C2 {4 L/ d7 K! ]5 U9 N9 t
       "where cur.reference like '1%'"5 L+ l7 r% F5 ]4 p" [) t3 Y
'this works ------------------2 k6 X, b7 P: N; T3 h
sSql = "select pre.reference  " & vbCrLf & _9 i* |  ?3 D0 u" ]: D# ~- G2 }
       "from pre IN '" & wbPrev.FullName & "' " & xl12 & vbCrLf & _* r+ r; G9 ?% ^) q9 m& y; A
       "where pre.reference like '1%'"
+ j9 U+ e* G+ e- F) u8 @. N'not working: syntax error in FROM clause. G4 D7 o2 G" b7 z. t
'sSql = "select cur.reference from  cur " & vbCrLf & _6 {+ V5 \2 {- w7 R/ ?
       "inner join pre IN '" & wbPrev.FullName & "' " & xl12 & " on cur.reference = pre.reference " & vbCrLf & _0 P' ~( H# l" H6 ]1 c2 l
       "where cur.reference like '1%'". R# ~! _4 i$ R' t! O
Debug.Print Now, sSql. q; G* C5 L  D9 Q6 N
Set rs = con.Execute(sSql)
7 t1 B# d& T3 J               
; U6 D& Z' U8 U解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则