|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
这个有点费脑。
Sub limonet()
Dim Cn As Object, StrSQL$, StrSQL1$, Sht As Worksheet
Set Cn = CreateObject("Adodb.Connection")
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
StrSQL1 = "Select * From [产品分类$] Union All Select 特殊编码,'特殊' as 分类,[箱/托] From [特殊产品$]"
For Each Sht In Worksheets
If Sht.Index > 3 Then
If Sht.Name = "其他" Then
StrSQL = "Select 代码,Null,供应商,编码,数量 As 箱数 From [发货数据$] Where 编码 Not In(Select 编码 From (" & StrSQL1 & "))"
ElseIf Sht.Name Like "特殊*" Then
StrSQL = "Select * From (" & StrSQL1 & ") Where 分类='" & Left(Sht.Name, 2) & "'"
StrSQL = "Select 代码,Null,供应商,b.编码,int(数量/包装率)+IIF(数量/包装率>int(数量/包装率),1,0) As 箱数 From (" & StrSQL & ")a Left Join [发货数据$]b On a.编码=b.编码 Where Not b.编码 is Null"
Else
StrSQL = "Select * From (" & StrSQL1 & ") Where 分类='" & Left(Sht.Name, 2) & "'"
StrSQL = "Select 代码,Null,供应商,b.编码,数量/包装率 As 箱数,分类 From (" & StrSQL & ")a Left Join [发货数据$]b On a.编码=b.编码 Where Not b.编码 is Null"
End If
Sht.Range("A2").CopyFromRecordset Cn.Execute(StrSQL)
End If
Next Sht
End Sub |
|