以下是引用sgrshh29在2008-9-8 10:15:00的发言:这个还有些问题,如果数据断月(某个月没有数据)?,如果跨年度? 如果数据断月(某个月没有数据)?,已解决 如果跨年度?已解决 新代码可查询不同月份和年度 Sub fig8() Set x = CreateObject("adodb.connection") x.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName kk = Year(Application.Max(Sheet1.[a2:a25])) & Right(100 + Month(Application.Max(Sheet1.[a2:a25])), 2) ll = Year(Application.Min(Sheet1.[a2:a25])) & Right(100 + Month(Application.Min(Sheet1.[a2:a25])), 2) For mm = ll To kk sql5 = "select '" & mm & "' as mm,商品编号 as ss,sum(进货数量)-sum(销售数量) as tt from [商品进出库期初期末$a1:d25] where year(日期)&right(100+month(日期),2)<=" & mm & " group by 商品编号 " sql6 = sql5 & " union all " & sql6 If Right(mm, 2) = 12 And Left(ll, 4) <> Left(kk, 4) Then mm = Left(ll, 4) + 1 & "00" End If
Next sql6 = Left(sql6, Len(sql6) - 10) sql7 = " select year(日期)&right(100+month(日期),2) as mm,商品编号 as ss, sum(进货数量) as vv,sum(销售数量) as ww from [商品进出库期初期末$a1:d25] group by year(日期)&right(100+month(日期),2), 商品编号" sql8 = " select aa.mm,aa.ss,aa.tt+iif(isnull(bb.ww),0,bb.ww)-iif(isnull(bb.vv),0,bb.vv),bb.vv,bb.ww ,aa.tt from (" & sql6 & ") aa left join (" & sql7 & ") bb on aa.mm&aa.ss=bb.mm&bb.ss order by aa.mm,aa.ss" Set y = x.Execute(sql8) Sheet1.[f15:z500].ClearContents Sheet1.[f15].CopyFromRecordset y End Sub
[此贴子已经被作者于2008-9-8 21:39:14编辑过] |