|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub sql()
- Dim sql As String, i
- Set conn = CreateObject("ADODB.Connection")
- Set rst = CreateObject("ADODB.Recordset"): Set dic = CreateObject("scripting.dictionary")
- conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
- sql = "select a.品项,b.对应产品名称值 from [结果$A2:A65536] a inner join [品项对照$] b on a.品项 = b.品项 "
- rst.Open sql, conn, 1, 3
- arr = Application.Transpose(rst.GetRows)
- ReDim brr(1 To UBound(arr), 1 To 10)
- For i = 1 To UBound(arr)
- s = arr(i, 1)
- If Not dic.exists(s) Then
- m = m + 1
- dic(arr(i, 1)) = m
- brr(m, 1) = arr(i, 1): brr(m, 2) = arr(i, 2)
- Else
- r = dic(arr(i, 1))
- For j = 2 To 10
- If brr(r, j) = "" Then brr(r, j) = arr(i, 2): Exit For
- Next
- End If
- Next i
- With Sheets("结果")
- .Range("a3").Resize(m, UBound(brr)) = brr
- End With
- rst.Close: conn.Close: Set rst = Nothing: Set conn = Nothing
- End Sub
复制代码 |
|