|
Sub limonet()
Dim Cn As Object, StrSQL$, StrSQL0$, StrSQL1$
Set Cn = CreateObject("Adodb.Connection")
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
StrSQL0 = "Select 号码 From [Sheet1$A:A] Group By 号码"
StrSQL = "Select cSNDefine4,cSNDefine3,cSNDefine9 From [Sheet2$] Where [cInvCode] Like '601%' And [cSNDefine9]<>''"
StrSQL = "Select First(cSNDefine4) As D4,Max(cSNDefine3) As D3,cSNDefine9 From (" & StrSQL & ") Group By cSNDefine9"
StrSQL = "Select 号码,D4 From (" & StrSQL0 & ")a Left Join (" & StrSQL & ")b On a.号码=b.cSNDefine9"
StrSQL1 = "Select cSNDefine4,cSNDefine9 From [Sheet2$] Where [cInvCode] Like '601%' And [cSNDefine9]<>'' And [cSNDefine4]<>'形态转换'"
StrSQL1 = "Select 号码,cSNDefine4 From (" & StrSQL0 & ")a Left Join (" & StrSQL1 & ")b On a.号码=b.cSNDefine9"
StrSQL = "Select a.号码,IIF(D4 is Null,cSNDefine4,D4),D4,cSNDefine4 From (" & StrSQL & ")a Left Join (" & StrSQL1 & ")b On a.号码=b.号码"
Range("A2").CopyFromRecordset Cn.Execute(StrSQL)
End Sub |
|