|
楼主 |
发表于 2019-1-23 17:02
|
显示全部楼层
感谢各位,刚才那个问题我自己想明白了。可以通过多重连接来实现三张表left join查询。
但另外两个问题困扰我,请帮忙看看。
问题1:我查询之后的表头怎么样弄到。
问题2:如果我的excel表里面的字段比较奇怪,要怎么样编写语句。例如字段名为:“原值(综合本位币):余额”
自己修改之后的代码如下:
- Sub ado方法()
- Set conn = CreateObject("ADODB.Connection") '
- Set rst = CreateObject("ADODB.Recordset") '
- conn.Open "dsn=excel files;dbq=" & ThisWorkbook.FullName
- bm1 = "select 资产编码 from [期初清单$] union all select 资产编码 from [期末清单$] union all select 资产编码 from [明细账$]"
- bm = "select distinct 资产编码 from (" & bm1 & ")"
- s1 = "select 资产编码,原值本币 as bg原值,累计折旧 As bg折旧 from [期初清单$]"
- s2 = "select 资产编码,原值本币 as bg原值,累计折旧 As bg折旧 from [期末清单$]"
- s3 = "select 资产编码,原值(综合本位币):借方金额 as dr原值,'原值(综合本位币):贷方金额' As cr折旧,'累计折旧(综合本位币):借方金额' as dr折旧 from [明细账$]"
- s4 = "select t1.资产编码 as 资产编码,* from (" & bm & ") t1 left join (" & s1 & ") t2 on t1.资产编码=t2.资产编码"
- s5 = "select * from (" & s4 & ") t3 left join (" & s2 & ") t4 on t3.资产编码=t4.资产编码"
- s = "select * from (select t1.资产编码 from (" & bm & ") t1 left join (" & s1 & ") t2 on t1.资产编码=t2.资产编码) t1 left join (" & s1 & ") t2 on t1.资产编码=t2.资产编码"
- s = "select * from (" & bm & ") t1,(" & s1 & ") t2,(" & s2 & ") t3 where t1.资产编码=t2.资产编码 and t1.资产编码=t3.资产编码"
- Set rst = conn.Execute(s5)
- Sheets("表").[a2].CopyFromRecordset rst
- end sub
复制代码
|
|