|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub qs()
- Dim Sql$, i&, a
- Application.ScreenUpdating = False
- Set cnn = CreateObject("adodb.connection")
- Set rst = CreateObject("ADODB.RecordSet")
- With cnn
- .Open "Provider=Microsoft.Ace.oledb.12.0;extended properties='excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- Sql = "select 型号 from [源$a:c] GROUP BY 型号 "
- End With
- rst.Open Sql, cnn, 1, 3
- a = Application.WorksheetFunction.Transpose(rst.GetRows)
- Set cnn2 = CreateObject("adodb.connection")
- Set rst2 = CreateObject("ADODB.RecordSet")
- With cnn2
- .Open "Provider=Microsoft.Ace.oledb.12.0;extended properties='excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- Sql2 = "select 区域 from [源$a:c] GROUP BY 区域 "
- End With
- rst2.Open Sql2, cnn, 1, 3
- b = Application.WorksheetFunction.Transpose(rst2.GetRows)
- Set rst2 = Nothing: Set cnn2 = Nothing
- Set rst = Nothing: Set cnn = Nothing
- ReDim brr(1 To UBound(a) + 1, 1 To UBound(b) + 1)
- For i = 2 To UBound(brr)
- brr(i, 1) = a(i - 1, 1)
- Next
- For j = 2 To UBound(brr, 2)
- brr(1, j) = b(j - 1, 1)
- Next j
- Dim arr: arr = Sheet1.Range("a1").CurrentRegion.Value
- Set dic = CreateObject("scripting.dictionary")
- For i = 2 To UBound(arr)
- s = arr(i, 1) & arr(i, 2)
- If Not dic.exists(s) Then
- dic(s) = arr(i, 3)
- Else
- dic(s) = dic(s) & "," & arr(i, 3)
- End If
- Next
- For i = 2 To UBound(brr)
- For j = 2 To UBound(brr, 2)
- s = brr(i, 1) & brr(1, j)
- If dic.exists(s) Then
- brr(i, j) = dic(s)
- End If
- Next
- Next
- Sheet5.Range("a1").Resize(UBound(brr), UBound(brr, 2)) = brr
- Set dic = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|