|
ZCJ521 发表于 2012-7-21 18:29
同一个工作表中,包装批次不会有重复的。但二个工作表的包装批次,特殊情况下就有可能有重复的,所以在信 ...
哦,要对付的就是不同的工作表中可能会有相同的包装批次,修改如下- Sub Macro1()
- Dim cnn As Object, SQL$, s$, temp$, arr, i&, rng As Range, d As Object, k, t
- Set d = CreateObject("scripting.dictionary")
- Set rng = Selection
- If rng.Columns.Count = 1 And Not Intersect(rng, [h4:h60000]) Is Nothing Then
- arr = rng.Offset(, -3).Resize(, 4)
- For i = 1 To UBound(arr)
- If Len(arr(i, 1)) Then
- If Not d.Exists(arr(i, 1)) Then d(arr(i, 1)) = "'" & arr(i, 4) & "'" Else d(arr(i, 1)) = d(arr(i, 1)) & ",'" & arr(i, 4) & "'"
- End If
- Next
- k = d.keys
- t = d.items
- If d.Count > 0 Then
- For i = 0 To d.Count - 1
- If SQL1 <> "" Then SQL1 = SQL1 & " union all "
- SQL1 = SQL1 & "select * from [" & k(i) & "$b4:n] where f1 in (" & t(i) & ")"
- Next
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0;hdr=no';data source=" & ThisWorkbook.Path & "\数据明细表.xlsx"
- If rng.Rows.Count = 1 Then temp = rng.Address(0, 0) & ":" & rng.Address(0, 0) Else temp = rng.Address(0, 0)
- s = "select f1 from [Excel 12.0;hdr=no;Database=" & ThisWorkbook.FullName & "].[信息总表$" & temp & "]"
- SQL = "select b.f2,b.f3,b.f4,b.f5,b.f6,b.f7,b.f8,b.f9,b.f10,b.f11,b.f12,b.f13 from (" & s & ") a left join (" & SQL1 & ") b on a.f1=b.f1"
- rng.Offset(, 1).Cells(1).CopyFromRecordset cnn.Execute(SQL)
- cnn.Close
- Set cnn = Nothing
- Else
- MsgBox "请选择化验批号"
- End If
- End If
- End Sub
复制代码 |
|