|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim d As New Dictionary
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim sql As String
- Dim mybook As String
- Dim arr, brr(), crr()
- mybook = ThisWorkbook.FullName
- With cnn
- If Application.Version = "11.0" Then
- .Provider = "microsoft.jet.oledb.4.0"
- .ConnectionString = "extended properties=""excel 8.0;HDR=YES;"";data source=" & mybook
- Else
- .Provider = "microsoft.ACE.oledb.12.0"
- .ConnectionString = "extended properties=""excel 12.0;HDR=YES;"";data source=" & mybook
- End If
- .Open
- End With
- sql = "select 车次单号,调拨出货仓,单据出货仓,单据收货仓,[卸货点1],[卸货点2],[卸货点3],车型,物流类型,sum(方数) as 方数,采购运费 from [明细$a1:p] group by 车次单号,调拨出货仓,单据出货仓,单据收货仓,[卸货点1],[卸货点2],[卸货点3],车型,物流类型,采购运费"
- arr = cnn.Execute(sql).GetRows()
- ReDim brr(1 To UBound(arr, 2) + 1, 0 To UBound(arr) + 1)
- For i = 0 To UBound(arr)
- For j = 0 To UBound(arr, 2)
- brr(j + 1, i + 1) = arr(i, j)
- Next
- Next
-
- For i = 1 To UBound(brr)
- d(brr(i, 1)) = d(brr(i, 1)) + 1
- Next
- mm = Application.Max(d.Items)
- ReDim crr(1 To d.Count, 1 To UBound(brr, 2) + mm - 1)
- d.RemoveAll
- m = 0
- For i = 1 To UBound(brr)
- If Not d.Exists(brr(i, 1)) Then
- m = m + 1
- d(brr(i, 1)) = m
- For j = 1 To 10
- crr(m, j) = brr(i, j)
- Next
- crr(m, UBound(crr, 2)) = brr(i, 11)
- Else
- n = d(brr(i, 1))
- For j = 10 To 9 + mm
- If Len(crr(n, j)) = 0 Then
- crr(n, j) = brr(i, 10)
- Exit For
- End If
- Next
- End If
- Next
-
- With Worksheets("结果")
- .Cells.Clear
- .Range("a1:i1") = Split("车次单号,调拨出货仓,单据出货仓,单据收货仓,[卸货点1],[卸货点2],[卸货点3],车型,物流类型", ",")
- For j = 1 To mm
- .Cells(1, 9 + j) = "方数" & j
- Next
- .Cells(1, UBound(crr, 2)) = "采购运费"
- .Range("a2").Resize(UBound(crr), UBound(crr, 2)) = crr
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- With .Range("a1").Resize(r, UBound(crr, 2))
- .Borders.LineStyle = xlContinuous
- End With
- End With
- End Sub
复制代码 |
|