回答

收藏

从宏查询(加入)外部wokbooks

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

我有3个Excel工作簿:+ @4 E9 n# V7 o/ S9 J0 ~  I2 U7 G
宏(完成后将作为外接程序)
5 ?/ U- ?2 b$ p$ ]- D0 H3 c0 ?) ^前期数据清单
* |3 W" |. F7 d  Q( @当前期间数据列表2 d5 k% I7 l% @/ Z( c
这两个列表均少于1000行。! W* t; Z( r: {. P' Y2 R8 {

/ l8 K5 l4 f5 G% P我正在尝试join在这些列表之间创建一个SQL ,但无法正常工作。由于工作表名称中包含点(例如“ TB 03.18”),因此我使用了技巧-" P* t: }- B3 X6 r8 C5 X- Z
在SO的某处找到-在相应的工作簿中定义命名范围。
, U; V5 w  \: L. b7 R7 Y- ~# J, }下面的代码包含有效的2条SQL信息和给出错误的1条信息。我搞不清楚了。* D5 |2 v; t. a5 t0 W3 A
'set connection to 'current tb'
. o3 a/ M' V4 ZsConn = &quotrovider=Microsoft.ACE.OLEDB.12.0;Data Source=" & wbCurr.FullName & _
- u4 D3 D& A. M6 m) l        ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"+ n* W- e% d% a
Set con = New ADODB.Connection
+ a, P5 R& T* [4 A, Q% y8 lcon.Open sConn' w8 y5 f6 M1 G: y  D, {" o- a) K
wbCurr.Names.Add Name:="cur", RefersTo:=shCurr.Range("A4").CurrentRegion    'to be made dynamic !!!  j: F; g* M0 u% F! i* c$ d: W
wbPrev.Names.Add Name:="pre", RefersTo:=shPrev.Range("A9").CurrentRegion    'to be made dynamic !!!
' L" s3 k1 _6 m  A2 ~lastRow = shPrev.Range("a15000").End(xlUp).Row
0 E/ ?" `# \2 U1 H: @shCurr.Range("F5:I15000").Clear
, W% J) j8 s: SDim xl12 As String: xl12 = "[Excel 12.0rovider=Microsoft.ACE.OLEDB.12.0;Extended Properties='HDR=YES';] "" U* a9 I1 _# o( E5 F- j2 L# L  ?
6 @. Q$ C- r, u" y0 H" P) ^
'this works ------------------
2 G8 X) e+ u8 L2 k1 ]: L' R7 I'sSql = "select reference from  cur " & vbCrLf & _
8 E8 J3 x3 V% d9 @7 J: `# n       "where cur.reference like '1%'"
, W3 ]% H! A8 v9 W'this works ------------------6 n+ X' p1 O: C, W
sSql = "select pre.reference  " & vbCrLf & _
! R+ z7 e% s/ M% @/ X8 j8 q/ `6 Z       "from pre IN '" & wbPrev.FullName & "' " & xl12 & vbCrLf & _
) C( J4 f; n$ h0 |& Q0 s: Q8 l       "where pre.reference like '1%'"
' q; `) J$ r% e! [4 }'not working: syntax error in FROM clause
; g2 Q8 H8 I+ T* ?; ['sSql = "select cur.reference from  cur " & vbCrLf & _
6 Q- q; u  u( t- Y' L       "inner join pre IN '" & wbPrev.FullName & "' " & xl12 & " on cur.reference = pre.reference " & vbCrLf & _* m0 S. K# y9 [- T/ n
       "where cur.reference like '1%'"
& N6 `# p4 d1 U$ j/ pDebug.Print Now, sSql
: r) T5 ^; Z+ f; PSet rs = con.Execute(sSql)
- A* l. y# S- n% M5 _% y6 p                . t; H, i' R( b! m% p/ \& t
解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则