|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub GetNewSht()
- Dim Cn As Object, Sql$
- Dim rng As Range
- Set Cn = CreateObject("Adodb.Connection")
- Cn.Open "DSN=Excel Files;DBQ=" & ThisWorkbook.FullName
- Sql = "Select T1.*,T2.成品编号,T2.材料编号,T2.材料均价,T2.材料单耗 " _
- & "From [实际成本$] T1 Left Join " _
- & "[预算成本$] T2 " _
- & "On T1.材料编号 = T2.材料编号"
- Sql = Sql & " Union (" _
- & "Select T1.*, T2.成品编号,T2.材料编号,T2.材料均价,T2.材料单耗 " _
- & "From [实际成本$] T1 Right Join " _
- & "[预算成本$] T2 " _
- & "On T1.材料编号=T2.材料编号)"
- With Sheets(3)
- .[2:65536].Delete
- .[A2].CopyFromRecordset Cn.Execute(Sql)
- For Each rng In .Range("A2", .[A65536].End(3))
- If Len(rng) = 0 Then rng = rng(1, 6): rng(1, 2) = rng(1, 7)
- rng(1, 6).Resize(1, 2).Delete 1
- Next
- End With
- Cn.Close: Set Cn = Nothing
- End Sub
复制代码 |
|