各位大神帮忙看看这段代码,哪儿出了问题,[提成发放]查询出来的结果会自动加一,研究了好几天了,百思不得其解:
Sub 统计数据()
On Error Resume Next
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties=""excel 12.0;hdr=yes"";data source=" & ThisWorkbook.FullName
'获得日期
年月 = Sheet2.Cells(Rows.Count, 1).End(xlUp).Value
'以下代码解决提成扣回中同一人将离职日期统一填充
sql5 = "select distinct 员工编码 from [提成扣回$] where 日期=" & "#" & 年月 & "# and 离职时间 is not null"
Set rs = CreateObject("adodb.recordset")
rs.Open sql5, cnn, 1, 3
For i = 1 To rs.RecordCount
a = rs.Fields(0)
Sheets("提成扣回").[a1].AutoFilter 12, a
Sheets("提成扣回").[a1].AutoFilter 1, Format(年月, "yyyy/m/d")
Set rg = Sheets("提成扣回").Cells(Sheets("提成扣回").Rows.Count, 27).End(xlUp)
rg.Copy Application.Intersect(Sheets("提成扣回").UsedRange.SpecialCells(12), Sheets("提成扣回").Range("aa2:aa100000"))
rs.MoveNext
Next i
Sheets("提成扣回").AutoFilterMode = False
'获得提成扣回sheet页数据集
sql6 = "select 业务单元,一级单位,二级单位,三级单位,四级部门,姓名,员工编码,提成金额,日期,离职时间 from [提成扣回$] where 日期=" & "#" & 年月 & "#"
'联合查询数据集中汇总聚合提成金额
sql7 = "select 姓名,员工编码,日期,离职时间,sum(提成金额) as 负提成金额 from (" & sql6 & ") group by 姓名,员工编码,日期,离职时间"
'获得正数提成发放记录集
sql8 = "select 姓名,员工编码,日期,离职时间,sum(提成金额) as 正提成金额 from [提成发放$] where 日期=" & "#" & 年月 & "#" & " group by 姓名,员工编码,日期,离职时间"
'正数与负数提成结合,确定提成实扣金额及其他金额
sql9 = "select a.姓名 as 姓名,a.员工编码 as 员工编码,a.离职时间 as 离职时间,a.日期 as 日期,round(b.正提成金额,2) as 提成发放,round(负提成金额,2) as 应扣,iif(b.离职时间 is not null or a.离职时间 is not null ,round(负提成金额,2),iif(iif(b.正提成金额 is null,0,b.正提成金额)*0.5<负提成金额*(-1) ,round(b.正提成金额*(-0.5),2),round(负提成金额,2))) as 实扣,应扣-iif(实扣 is null,0,实扣) as 待扣 from (" & sql7 & ") as a left join (" & sql8 & ") as b on a.姓名&a.员工编码=b.姓名&b.员工编码"
sql10 = "select 业务单元,一级单位,二级单位,三级单位,四级部门,姓名,员工编码,round(sum(提成金额),2) as 新增提成,日期,离职时间 from [提成扣回$] where 项目名称(一级)<>'历史提成退款数据' and 日期=#" & 年月 & "# group by 业务单元,一级单位,二级单位,三级单位,四级部门,姓名,员工编码,日期,离职时间"
sql11 = "select b.业务单元,b.一级单位,b.二级单位,b.三级单位,b.四级部门,a.姓名,a.员工编码,null as 岗位名称,null as 职衔,null as 入职日期,format(a.离职时间,'yyyy/m/d'),null as 身份证号,null as 职级,a.日期,a.提成发放,b.新增提成,a.应扣,a.实扣,a.待扣 from (" & sql9 & ") as a left join (" & sql10 & ") as b on a.姓名&a.员工编码=b.姓名&b.员工编码"
'删除台账中已有月份数据
Sheets("台账").Range("a1").AutoFilter 14, Format(年月, "yyyy/m/d")
Set rg = Sheets("台账").Range("a1").SpecialCells(xlVisible)
Application.Intersect(Sheets("台账").Rows("2:200000"), rg).Delete
Sheets("台账").AutoFilterMode = False
'清空原有累计数据
Set wd = Sheets("台账").Columns("a:a").Find("累计")
If Not wd Is Nothing Then wd.EntireRow.Delete
'加入本月台账数据
m = Sheets("台账").[a1].CurrentRegion.Rows.Count
'Cells(1, 1).CopyFromRecordset cnn.Execute(sql8)
Sheets("台账").Cells(m + 1, 1).CopyFromRecordset cnn.Execute(sql11)
m = Sheets("台账").[a1].CurrentRegion.Rows.Count
'添加累计
Sheets("台账").Cells(m + 1, 1) = "累计"
For i = 1 To 5
Sheets("台账").Cells(m + 1, 14 + i).Formula = "=Subtotal(109," & Sheets("台账").Cells(2, 14 + i).Address(0, 0) & ":" & Sheets("台账").Cells(m, 14 + i).Address(0, 0) & ")"
Next i
Sheets("台账").Cells(m + 1, 1).EntireRow.Font.Bold = True
Sheets("台账").Range("a1").CurrentRegion.Borders.LineStyle = xlContinuous
Set cnn = Nothing
End Sub |