|
本帖最后由 zhaogang1960 于 2016-2-17 15:48 编辑
这类问题我一般用字典处理,用SQL语句处理wuxiang_123版主比较在行
下面可能是最笨的SQL方法了,请参考:
Sub 宏1()
Dim cnn As Object, SQL$, t1$, t2$, t3$, t4$
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider = Microsoft.ace.Oledb.12.0;Extended Properties =Excel 12.0;Data Source =" & ThisWorkbook.FullName
t1 = "[出库表$" & Sheets("出库表").Range("A1").CurrentRegion.Address(0, 0) & "]"
t2 = "[采购价格表$" & Sheets("采购价格表").Range("b4").CurrentRegion.Address(0, 0) & "]"
t3 = "(Select b.* from " & t1 & " a," & t2 & " b where a.名称=b.名称 and a.日期>=b.日期)"
t4 = "Select x.* from " & t3 & " x inner join (select 名称,max(日期) as max_date from " & t3 & " group by 名称) y on x.名称=y.名称 and x.日期=y.max_date"
SQL = "select 单价 from " & t1 & " m left join (" & t4 & ") n on m.名称=n.名称"
[c2].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
End Sub
|
评分
-
1
查看全部评分
-
|