|
- Sub ToExcel()
- Dim Sql$, Sql1$, FilesPath$, FielsName, arr, i, j
- Dim Cnn As Object
- FilesPath = ThisWorkbook.Path & Application.PathSeparator
- Set Cnn = CreateObject("Adodb.Connection")
- FielsName = [{"数据源1.xls","记录.xls","Access记录.mdb";"数据源2.xls","记录2.xls","Access记录2.mdb"}]
- For j = 1 To UBound(FielsName)
- Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0';Data Source=" & FilesPath & FielsName(j, 1)
- arr = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Cnn.Execute("Select Distinct 商品编号 from [Sheet1$]").GetRows))
- If Dir(FilesPath & FielsName(j, 2)) <> "" Then
- MsgBox FilesPath & FielsName(j, 2) & "已存在,请删除后再测试本程序"
- Exit Sub
- End If
- For i = 1 To UBound(arr)
- On Error GoTo Errhandle
- '为符合条件的数据加上序号,1为起始数
- Sql = "Select b.日期,b.送货单位,b.商品编号,(Select count(1) from [Sheet1$]as a Where a.商品编号=b.商品编号 and a.日期<=b.日期 ) as 排名 from [Sheet1$] as b Where 商品编号='" & arr(i) & "' "
- '为符合条件的数据加上错位序号,2为起始数
- Sql1 = "Select b.日期,b.送货单位,b.商品编号,(Select count(1)+1 from [Sheet1$]as a Where a.商品编号=b.商品编号 and a.日期<=b.日期 ) as 排名 from [Sheet1$] as b Where 商品编号='" & arr(i) & "' "
- '利用错位序号,得出要剔除的日期
- Sql = "Select b.日期 from (" & Sql & ") as a Left Join (" & Sql1 & ") as b on a.排名=b.排名 and a.送货单位=b.送货单位"
- '去除空值
- Sql = "Select * from (" & Sql & ") Where 日期 is not null "
- '得出最终结果
- Sql = "Select a.* from [Sheet1$] as a Left Join (" & Sql & ") as b on a.日期=b.日期 Where b.日期 is null and 商品编号='" & arr(i) & "' "
- '把最终结果写入相应文件
- Sql = "Select * into [Excel 8.0;Database=" & FilesPath & FielsName(j, 2) & "].[" & arr(i) & "] FROM (" & Sql & ")"
- '执行
- Cnn.Execute (Sql)
- Next i
- Cnn.Close
- Next j
- Set Cnn = Nothing: Set Cat = Nothing
- Exit Sub
- Errhandle:
- MsgBox Err.Description
- End Sub
- Sub ToAccess()
- Dim Sql$, Sql1$, FilesPath$, FielsName, arr, i, j
- Dim Cnn As Object, Cat As Object
- FilesPath = ThisWorkbook.Path & Application.PathSeparator
- Set Cnn = CreateObject("Adodb.Connection")
- Set Cat = CreateObject("ADOX.Catalog")
- FielsName = [{"数据源1.xls","记录.xls","Access记录.mdb";"数据源2.xls","记录2.xls","Access记录2.mdb"}]
- For j = 1 To UBound(FielsName)
- Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0';Data Source=" & FilesPath & FielsName(j, 1)
- arr = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Cnn.Execute("Select Distinct 商品编号 from [Sheet1$]").GetRows))
- If Dir(FilesPath & FielsName(j, 3)) <> "" Then
- MsgBox FilesPath & FielsName(j, 3) & "已存在,请删除后再测试本程序"
- Exit Sub
- Else
- Cat.CREATE "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & FilesPath & FielsName(j, 3)
- End If
- For i = 1 To UBound(arr)
- On Error GoTo Errhandle
- '为符合条件的数据加上序号,1为起始数
- Sql = "Select b.日期,b.送货单位,b.商品编号,(Select count(1) from [Sheet1$]as a Where a.商品编号=b.商品编号 and a.日期<=b.日期 ) as 排名 from [Sheet1$] as b Where 商品编号='" & arr(i) & "' "
- '为符合条件的数据加上错位序号,2为起始数
- Sql1 = "Select b.日期,b.送货单位,b.商品编号,(Select count(1)+1 from [Sheet1$]as a Where a.商品编号=b.商品编号 and a.日期<=b.日期 ) as 排名 from [Sheet1$] as b Where 商品编号='" & arr(i) & "' "
- '利用错位序号,得出要剔除的日期
- Sql = "Select b.日期 from (" & Sql & ") as a Left Join (" & Sql1 & ") as b on a.排名=b.排名 and a.送货单位=b.送货单位"
- '去除空值
- Sql = "Select * from (" & Sql & ") Where 日期 is not null "
- '得出最终结果
- Sql = "Select a.* from [Sheet1$] as a Left Join (" & Sql & ") as b on a.日期=b.日期 Where b.日期 is null and 商品编号='" & arr(i) & "' "
- '把最终结果写入相应文件
- Sql = "Select * into [;Database=" & FilesPath & FielsName(j, 3) & "]." & arr(i) & " FROM (" & Sql & ")"
- '执行
- Cnn.Execute (Sql)
- Next i
- Cnn.Close
- Next j
- Set Cnn = Nothing: Set Cat = Nothing
- Exit Sub
- Errhandle:
- MsgBox Err.Description
- End Sub
复制代码
搭个末班车,昨天才注意到正式竞赛区84期的这道题目,晚了点,不过幸亏搭上车了,呵呵。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|