|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test()
- Dim cnn As Object, arr, brr(), i&
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
- mysql = "select 区域,数据,sum(数量) from [原表$] group by 区域,数据 order by 区域 asc"
- arr = WorksheetFunction.Transpose(cnn.Execute(mysql).getrows)
- ReDim brr(1 To UBound(arr), 1 To 2)
- cnn.Close
- mysum = 0: Sum = 0
- For i = 1 To UBound(arr) - 1
- If mysum = 0 Then mysum = arr(i, 3)
- If arr(i, 1) = arr(i + 1, 1) Then
- mysum = mysum + arr(i + 1, 3)
- Else
- brr(i, 1) = mysum
- mysum = 0
- End If
- Sum = Sum + arr(i, 3)
- Next i
- brr(i, 1) = mysum: brr(i, 2) = Sum + arr(i, 3)
- Sheet2.Range("b3").Resize(UBound(arr), 3) = arr
- Sheet2.Range("e3").Resize(UBound(brr), 2) = brr
- End Sub
复制代码 |
|